最近在做一个不固定行列的excel导出。由于之前都是用公司封装的导出功能,直接传一个list对象进去,具体实现也不用知道。但是这个需求比较特殊,行和列都是不固定的,前台选择一个证券组合和一段日期,导出该组合,这段时间内的证券行情。组合里的证券个数不一,日期天数也不一,调用封装的方法无法实现,便自己随便写了个excel导出。
上代码:
import java.util.Arrays;
import java.util.List;
/**
* 需要导出的对象
*/
public class SecuQuoHisRole {
private String quoTypeName;//行情类型名称
private String[] secuInfo;//证券信息
private String[][] secuQuoHisInfo;//证券行情信息
private List<String> secuDate;//日期
//此处省略类get/set方法等...
}
/**
* 返回导出文件名
* @param role
* @param exportType:excel文件后缀
* @return
*/
public String queryExport(SecuQuoHisRole role, String exportType) {
String fileUrl = Constants.FILE_ROOTPATH+ConstantsExport.EXPORTMENU;
File saveDirFile = new File(fileUrl);
if (!saveDirFile.exists() || !saveDirFile.isDirectory()) {
saveDirFile.mkdirs();
}
String fileurl= "D:\\temp\\upload\\tempExportFile\\";
String fileName= "证券组合市场数据.xlsx";
try {
OutputStream os = new FileOutputStream(new File(fileurl + fileName));
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("证券组合市场数据");// 创建一个sheet
sheet.setDefaultColumnWidth(20);//默认列宽20
//首行样式
XSSFCellStyle style = wb.createCellStyle();// 设置这些样式
style.setFillForegroundColor(HSSFColor.DARK_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
XSSFFont font = wb.createFont();// 生成一个字体
font.setColor(HSSFColor.WHITE.index);
font.setFontHeightInPoints((short) 12);
font.setFontName("Times New Roman");
style.setFont(font);// 把字体应用到当前的样式
//非首行样式
XSSFCellStyle style2 = wb.createCellStyle();// 设置这些样式
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
XSSFFont font2 = wb.createFont();// 生成一个字体
font2.setFontHeightInPoints((short) 11);
font2.setFontName("Times New Roman");
style2.setFont(font2);// 把字体应用到当前的样式
for (int i = 0; i < role.getSecuQuoHisInfo().length; i++) {
if(i == 0){// 生成第一行(表头)
XSSFRow row = sheet.createRow(i);
XSSFCell firstCell = row.createCell(i);
firstCell.setCellStyle(style);
firstCell.setCellValue("日期");
for(int j = 1; j <= role.getSecuInfo().length; j++ ){
XSSFCell otherCell = row.createCell(j);
otherCell.setCellStyle(style);
otherCell.setCellValue(role.getSecuInfo()[j-1]);
}
}else{
XSSFRow row = sheet.createRow(i);
XSSFCell firstCell = row.createCell(0);
firstCell.setCellStyle(style2);
firstCell.setCellValue(role.getSecuDate().get(i-1));
for(int k = 1; k <= role.getSecuQuoHisInfo()[0].length; k++ ){
XSSFCell otherCell = row.createCell(k);
otherCell.setCellStyle(style2);
otherCell.setCellValue(role.getSecuQuoHisInfo()[i][k-1]);
}
}
}
wb.write(os);// 写文件
os.close();// 关闭输出流
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
页面展示如下:
excel导出如下: