java-excel
public static void generateControllFile(List<List> infoLists) throws ParseException {
logger.info("正在生成xlsx管控文件");
String stationInfo_title[] ={
"序列号","用户名","管控文件","存储位置","供应商信息","签订时间","到期时间","公司内部联系人","邮箱","内部提醒时间","外部联系人","邮箱","外部提醒时间","邮件标题","邮件发送者"
};
//创建excel工作薄
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个工作表sheet
XSSFSheet sheet = workbook.createSheet("controInfo");
//创建第一行
XSSFRow row = sheet.createRow(0);
//创建一个单元格
XSSFCell cell = null;
//插入第一行标题栏
for (int i=0;i<stationInfo_title.length;i++) {
cell = row.createCell(i);
cell.setCellValue(stationInfo_title[i]);
}
for (int i = 0; i < infoLists.size(); i++) {
XSSFRow row1 = sheet.createRow(i+1);
List ll = infoLists.get(i);
for (int j = 0; j < ll.size(); j++) {
cell = row1.createCell(j);
if(ll.get(j).toString().contains("-")){
CellStyle cellStyle = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("m/d/yy"));
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
cell.setCellValue(format1.parse(ll.get(j).toString()));
cell.setCellStyle(cellStyle);
continue;
}
cell.setCellValue(ll.get(j).toString());
if(j==0){
cell.setCellValue(i+1);
}
}
}
File fileControllDir=new File(CommonConfig.getUploadFile_url()+"\\专用管控");
if(!fileControllDir.exists()){//如果文件夹不存在
fileControllDir.mkdir();//创建文件夹
}
File file = new File(CommonConfig.getUploadFile_url()+"\\专用管控\\import-varicut-contract-file.xlsx");
try {
if(!file.exists()) {
file.createNewFile();
}
//创建输出流
OutputStream outputStream = new FileOutputStream(file);
//将拼好的Excel写入到文件流
workbook.write(outputStream);
//关闭输出流
outputStream.close();
}catch (Exception e) {
e.printStackTrace();
}
}