eclipse webapp中相对路径获取和导出excel总结

 web app项目,文件一般都是放在src main webapp下,这里才相对于项目结构。

public void exportItTask(HttpServletResponse response, ItTask itTask,HttpServletRequest hsq){

//相对路径获取,其他都会写死,这个无论项目结构如何变,都可以使用
String savedDir = hsq.getServletContext().getRealPath("fileDir");

File file = new File(savedDir+"\aa.xlsx");

}

很多时候项目中都要导出excel,来做成报表。

两种方式:

1.一种是直接用代码生成excel,把数据写进去后,下载到服务器。(应用于excel数据简单,需求上没有什么图形,图表,单元格一些奇葩操作)

2.一种是先和需求人员商量好做好excel模板,然后直接读取excel文件进来,再向模板中填充数据,再下载下来(应用于excel数据复杂,有图表,图形等的)

//相对路径获取
String savedDir = hsq.getServletContext().getRealPath("fileDir");

File file = new File(savedDir+"\ 项目计划表.xlsx");

XSSFWorkbook workbook = null;
FileInputStream inputStream = new FileInputStream(file);
workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheet("项目计划表");//看自己哪页了

XSSFRow row1 = sheet.getRow(1);//获取到要填充的行之前内容
XSSFRow row2 = sheet.getRow(38);//获取到要填充的行之前内容
XSSFRow row3 = sheet.createRow(lists.size()+3);//在填完数据之后要填充的东西
row1.setHeight((short) (65 * 15.625));//第二行高度
Map<String, Object> mapps = new HashMap<>();
Map<String,Object> mapsLater = new HashMap<>();
//0,3,6,9
for(int i=0;i<12;i++){
XSSFCell rowA = row1.getCell(i);
XSSFCell rowB = row2.getCell(i);
if(null !=rowA){
String rowAStr = rowA.getStringCellValue();
mapps.put(i+"", rowAStr);
}
if(null !=rowB){
String rowBStr = rowB.getStringCellValue();
mapsLater.put(i+"", rowBStr);
}

}

//填充内容
XSSFCell cellA = row1.createCell(0);
cellA.setCellValue((String)mapps.get("0")+projectName);


XSSFCell cellB = row1.createCell(3);
cellB.setCellValue((String)mapps.get("3")+projectNum);

XSSFCell cellC = row1.createCell(6);
cellC.setCellValue((String)mapps.get("6")+projectManager);

XSSFCell cellD = row1.createCell(9);
cellD.setCellValue((String)mapps.get("9")+period);

XSSFCell cellE = row3.createCell(0);
cellE.setCellValue((String)mapsLater.get("0"));


XSSFCell cellF = row3.createCell(3);
cellF.setCellValue((String)mapsLater.get("4"));

XSSFCell cellG = row3.createCell(6);
cellG.setCellValue((String)mapsLater.get("9"));

单元格样式设置:

CreationHelper createHelper = workbook.getCreationHelper();

writeExcel.getCellStyle(workbook, createHelper);

该方法封装在writeExcel类中。

public class WriteExcel {

public XSSFCellStyle setBorderTan;
public XSSFCellStyle setBorderWhite;

  public void getCellStyle(XSSFWorkbook workbook, CreationHelper createHelper) {


XSSFCellStyle setBorderTan = workbook.createCellStyle();
setBorderTan.setBorderBottom(CellStyle.BORDER_THIN); //下边框
setBorderTan.setBorderLeft(CellStyle.BORDER_THIN);//左边框
setBorderTan.setBorderTop(CellStyle.BORDER_THIN);//上边框
setBorderTan.setBorderRight(CellStyle.BORDER_THIN);//右边框
setBorderTan.setAlignment(CellStyle.ALIGN_CENTER); // 居中
setBorderTan.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中
setBorderTan.setFillForegroundColor(IndexedColors.TAN.getIndex());
setBorderTan.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充背景色
setBorderTan.setWrapText(true);//设置自动换行
this.setBorderTan = setBorderTan;



XSSFCellStyle setBorderWhite = workbook.createCellStyle();
setBorderWhite.setBorderBottom(CellStyle.BORDER_THIN); //下边框
setBorderWhite.setBorderLeft(CellStyle.BORDER_THIN);//左边框
setBorderWhite.setBorderTop(CellStyle.BORDER_THIN);//上边框
setBorderWhite.setBorderRight(CellStyle.BORDER_THIN);//右边框
setBorderWhite.setAlignment(CellStyle.ALIGN_CENTER); // 居中
setBorderWhite.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 上下居中
// setBorderWhite.setFillForegroundColor(IndexedColors.WHITE.getIndex());//不填充默认白色
// setBorderWhite.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充背景色
setBorderWhite.setWrapText(true);//设置自动换行
this.setBorderWhite = setBorderWhite;

}

}

单元格合并

//阶段合并
if(startNumber<=endNumber){
sheet.addMergedRegion(new CellRangeAddress(startNumber,endNumber,1,1));//参数分别是 开始行,结束行,开始列,结束列
}

下载导出:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String str = formatter.format(LocalDateTime.now());
//String fileName = "userinfo";
logger.info("projectNum:"+deviceProjects.getProjectNum());
String fileName = deviceProjects.getProjectNum() + "-ss" + str;
response.setContentType("application/octet-stream");//设置返回的内容类型
// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
try {
response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"), "iso8859-1") + ".xlsx");
response.flushBuffer();
response.setCharacterEncoding("utf-8");
workbook.write(response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

小细节:

字符串中+“ ”  空格,空格是生效的。 

原文地址:https://www.cnblogs.com/yiyezhiqiuwuchen/p/12652210.html