当前位置:文档之家› 导出excel

导出excel

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import https://www.doczj.com/doc/4a5827347.html,mons.io.IOUtils;
import https://www.doczj.com/doc/4a5827347.html,ermodel.HSSFCellStyle;
import https://www.doczj.com/doc/4a5827347.html,ermodel.CellStyle;
import https://www.doczj.com/doc/4a5827347.html,ermodel.DataFormat;
import https://www.doczj.com/doc/4a5827347.html,ermodel.Row;
import https://www.doczj.com/doc/4a5827347.html,ermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import https://www.doczj.com/doc/4a5827347.html,ermodel.XSSFWorkbook;

public final class ExcelWriter {
public static final class Cell {
private Workbook wb;
private https://www.doczj.com/doc/4a5827347.html,ermodel.Cell target;

private Cell(Workbook wb, https://www.doczj.com/doc/4a5827347.html,ermodel.Cell cell) {
this.wb = wb;
this.target = cell;
}
public void setCellValue(String value) {
this.target.setCellValue(value);
}
public void setCellValue(Date value) {
if (null == value) {
this.setCellValue("未设置");
return;
}
CellStyle style = this.wb.createCellStyle();
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("yyyy/m/d hh:mm:ss"));
this.target.setCellStyle(style);
this.target.setCellValue(value);
}
public void setCellValue(double value) {
CellStyle style = this.wb.createCellStyle();
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("###,##0"));
this.target.setCellStyle(style);
this.target.setCellValue(value);
}

public void setCellStyle(CellStyle style){
this.target.setCellStyle(style);
}
}

public static final class Sheet{
private Workbook wb;
private https://www.doczj.com/doc/4a5827347.html,ermodel.Sheet sheet;
private Sheet(Workbook wb, https://www.doczj.com/doc/4a5827347.html,ermodel.Sheet sheet) {
this.sheet = sheet;
this.wb = wb;
}

public Cell getOrCreateCell(int rowBasedZero, int cellBasedZero) {
Row row = this.sheet.getRow(rowBasedZero);
if (null == row) {
row = this.sheet.createRow(rowBasedZero);
}
return new Cell(this.wb, row.getCell(cellBasedZero, Row.CREATE_NULL_AS_BLANK));
}

//合并单元格
public void mergedRegion(int intFirstRow, int intLastRow, int intFirstColumn, int intLastColumn){
this.sheet.addMergedRegion(new CellRangeAddress(intFirstRow, intLastRow, intFirstColumn, intLastColumn));
}

//添加居中的样式
public CellStyle addStyle(){
CellStyle style = wb.createCellStyle();
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.A

LIGN_CENTER);// 水平
return style;
}

}
private Map sheetMap = new HashMap();
private OutputStream out;
private Workbook wb;

public ExcelWriter(OutputStream out) {
this.out = out;
wb = new XSSFWorkbook();
}

public Sheet getOrCreateSheet(String sheet) {
Sheet sheet1 = this.sheetMap.get(sheet);
if (null != sheet1) {
return sheet1;
}
https://www.doczj.com/doc/4a5827347.html,ermodel.Sheet xlsSheet = this.wb.getSheet(sheet);
if (null == xlsSheet) {
xlsSheet = this.wb.createSheet(sheet);
}
Sheet obj = new Sheet(this.wb, xlsSheet);
this.sheetMap.put(sheet, obj);
return obj;
}

public void write() throws IOException {
this.wb.write(this.out);
}

public void close() {
this.sheetMap.clear();
IOUtils.closeQuietly(this.out);
}
}



service层处理:

public byte[] findExcelData() throws NetflowServiceException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ExcelWriter writer = null;
try {
writer = new ExcelWriter(out);
/*拼装excel头部*/
ExcelWriter.Sheet sheetAct = writer.getOrCreateSheet(informationPublishForm.getOrdertitle());

for(int i=0;isheetAct.getOrCreateCell(0, i).setCellValue(excelHead.get(i));
}

/*拼装excel内容*/
for(int i=0;isheetAct.getOrCreateCell((i + 1), j).setCellValue(value);
}
}
writer.write();
} catch(Exception e){
throw new NetflowServiceException(e);
} finally {
if (null != writer) {
writer.close();
}
}
return out.toByteArray();
}


action处理:

public StringdownExcel() {
try {
byte[] fileNameByte = (informationPublishForm.getOrdertitle() + ".xlsx").getBytes("GBK");
String filename = new String(fileNameByte, "ISO8859-1");

byte[] bytes = commonTableReportService.findExcelData(psForm, reportId);
response.setContentType("application/x-msdownload");
response.setContentLength(bytes.length);
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.getOutputStream().write(bytes);
https://www.doczj.com/doc/4a5827347.html,("产生excel文件大小(byte):" + bytes.length);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}

相关主题
文本预览
相关文档 最新文档