Java-POI-sxssfWorkbook实例
2017-09-28 15:30
323 查看
package com.herdsric.excel.create;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.common.type.NumberUtil;
public class ExcelCreateBigRecord {
private static Logger logger = LogManager.getLogger(ExcelCreateBigRecord.class.getName());
private SXSSFWorkbook sxssfWorkbook;
private CellStyle noramlStyle;
private CellStyle formatStyle1;
private CellStyle formatStyle2;
public ExcelCreateBigRecord(InputStream excelIs){
try {
XSSFWorkbook workbook = new XSSFWorkbook(excelIs);
this.sxssfWorkbook = new SXSSFWorkbook(workbook, 100);
this.noramlStyle = this.sxssfWorkbook.createCellStyle();
this.formatStyle1 = this.sxssfWorkbook.createCellStyle();
this.formatStyle2 = this.sxssfWorkbook.createCellStyle();
this.setNormalStyle();
this.setFormatStyle1();
this.setFormatStyle2();
} catch (IOException e) {
logger.error("创建sxssfWorkbook错误:"+e.getMessage());
}
}
private void setFormatStyle1(){
DataFormat df = this.sxssfWorkbook.createDataFormat();
this.formatStyle1.setDataFormat(df.getFormat("#,#0.00"));
}
private void setFormatStyle2(){
DataFormat df = this.sxssfWorkbook.createDataFormat();
this.formatStyle2.setDataFormat(df.getFormat("@"));
}
private void setNormalStyle(){
Font font = this.sxssfWorkbook.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 10);
this.noramlStyle.setFont(font);
this.formatStyle1.setFont(font);
this.formatStyle2.setFont(font);
}
public void createWorkBook(
List<Map<String,Object>> recordList,
int sheetNum,
int firstRow) throws IOException{
Sheet sheet = this.sxssfWorkbook.getSheetAt(sheetNum);
for(int i = 0; i < recordList.size(); i++){
Row row = sheet.createRow(i+firstRow);
Map<String,Object> rowMap = recordList.get(i);
for(int j = 0; j < rowMap.size(); j++){
Cell cell = row.createCell(j);
Object celVal = rowMap.get("e"+(j+1));
this.setCellValue(cell, celVal);
}
}
}
public void removeSheet(int sheetNum){
this.sxssfWorkbook.removeSheetAt(sheetNum);
}
public void outWrite(OutputStream out){
try {
this.sxssfWorkbook.write(out);
} catch (IOException e) {
logger.error("sxssfWorkbook输出错误:"+e.getMessage());
}
}
private void setCellValue(Cell cell,Object celVal){
if(celVal == null){
cell.setCellValue("");
cell.setCellStyle(this.noramlStyle);
}else if(celVal.toString().indexOf("mil") != -1){
String cel = celVal.toString().replaceAll("mil", "").trim();
if(NumberUtil.isNumeric(cel)){
cell.setCellValue(new BigDecimal(cel).doubleValue());
cell.setCellStyle(this.formatStyle1);
}else{
cell.setCellValue(cel);
cell.setCellStyle(this.noramlStyle);
}
}else if(NumberUtil.isNumeric(celVal.toString().trim())){
cell.setCellValue(new BigDecimal(celVal.toString().trim()).doubleValue());
cell.setCellStyle(this.formatStyle1);
}else{
cell.setCellValue(celVal.toString().replaceAll("cus", ""));
cell.setCellStyle(this.formatStyle2);
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.common.type.NumberUtil;
public class ExcelCreateBigRecord {
private static Logger logger = LogManager.getLogger(ExcelCreateBigRecord.class.getName());
private SXSSFWorkbook sxssfWorkbook;
private CellStyle noramlStyle;
private CellStyle formatStyle1;
private CellStyle formatStyle2;
public ExcelCreateBigRecord(InputStream excelIs){
try {
XSSFWorkbook workbook = new XSSFWorkbook(excelIs);
this.sxssfWorkbook = new SXSSFWorkbook(workbook, 100);
this.noramlStyle = this.sxssfWorkbook.createCellStyle();
this.formatStyle1 = this.sxssfWorkbook.createCellStyle();
this.formatStyle2 = this.sxssfWorkbook.createCellStyle();
this.setNormalStyle();
this.setFormatStyle1();
this.setFormatStyle2();
} catch (IOException e) {
logger.error("创建sxssfWorkbook错误:"+e.getMessage());
}
}
private void setFormatStyle1(){
DataFormat df = this.sxssfWorkbook.createDataFormat();
this.formatStyle1.setDataFormat(df.getFormat("#,#0.00"));
}
private void setFormatStyle2(){
DataFormat df = this.sxssfWorkbook.createDataFormat();
this.formatStyle2.setDataFormat(df.getFormat("@"));
}
private void setNormalStyle(){
Font font = this.sxssfWorkbook.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short) 10);
this.noramlStyle.setFont(font);
this.formatStyle1.setFont(font);
this.formatStyle2.setFont(font);
}
public void createWorkBook(
List<Map<String,Object>> recordList,
int sheetNum,
int firstRow) throws IOException{
Sheet sheet = this.sxssfWorkbook.getSheetAt(sheetNum);
for(int i = 0; i < recordList.size(); i++){
Row row = sheet.createRow(i+firstRow);
Map<String,Object> rowMap = recordList.get(i);
for(int j = 0; j < rowMap.size(); j++){
Cell cell = row.createCell(j);
Object celVal = rowMap.get("e"+(j+1));
this.setCellValue(cell, celVal);
}
}
}
public void removeSheet(int sheetNum){
this.sxssfWorkbook.removeSheetAt(sheetNum);
}
public void outWrite(OutputStream out){
try {
this.sxssfWorkbook.write(out);
} catch (IOException e) {
logger.error("sxssfWorkbook输出错误:"+e.getMessage());
}
}
private void setCellValue(Cell cell,Object celVal){
if(celVal == null){
cell.setCellValue("");
cell.setCellStyle(this.noramlStyle);
}else if(celVal.toString().indexOf("mil") != -1){
String cel = celVal.toString().replaceAll("mil", "").trim();
if(NumberUtil.isNumeric(cel)){
cell.setCellValue(new BigDecimal(cel).doubleValue());
cell.setCellStyle(this.formatStyle1);
}else{
cell.setCellValue(cel);
cell.setCellStyle(this.noramlStyle);
}
}else if(NumberUtil.isNumeric(celVal.toString().trim())){
cell.setCellValue(new BigDecimal(celVal.toString().trim()).doubleValue());
cell.setCellStyle(this.formatStyle1);
}else{
cell.setCellValue(celVal.toString().replaceAll("cus", ""));
cell.setCellStyle(this.formatStyle2);
}
}
}
相关文章推荐
- POI操作Excel2007实例二之“SXSSFWorkbook”处理海量数据
- POI操作Excel2007实例二之“SXSSFWorkbook”处理海量数据
- POI操作Excel2007实例二之“SXSSFWorkbook”处理海量数据
- java使用SXSSFWorkbook生成具有图片与文字的Excel表格
- 异常:java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Workbook.getCellStyleAt
- Java基础——HSSFworkbook,XSSFworkbook,SXSSFworkbook区别简述
- Java的poi技术读取和导入Excel简单实例
- java 的poi导出excel实例
- 3.POI SXSSF导出大量数据实例
- 使用POI操作Excel时new XSSFWorkbook ()报错java.lang.NoSuchMethodError解决方案
- java版poi+excel导入实例1
- poi,java向 excel文件写数据(缺点,HSSFWorkbook不支持图片) 例子
- java用poi读写excel实例
- java通过Apache开源框架POI读取Word2003和Word2007转换成html的demo实例
- JAVA POI HSSFWorkbook 导出Excel表格超大数据量解决方案
- 最简单的Java导出excel表格(poi中的XSSFWorkbook方式)
- java使用poi读取excel内容方法实例
- 利用poi-SXSSFWorkbook导出数据
- java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook 错误来源
- Java 使用POI生成带联动下拉框的excel表格实例代码