String的join方法:
从JDK1.8开始,出现的方法,用来返回指定分隔符拼接起来的字符串;
delimiter:每个元素要添加的分隔符;
elements:需要添加分隔符的字符串;
String fileName = "租赁物清单模板.xlsx";
String pdfUrl = String.join(File.separator,"zlw",UUIDHexGenerator.generate(), fileName as String).replace("\\", "/");
String finalPath1 = String.join(File.separator, properties.getFileRootPath(), pdfUrl).replace("\\", "/");
//最终路径
String finalPath = finalPath1.replaceAll("//","/");
HSSFWorkbook:一般用于操作Excel2003以前(包括2003)的版本,扩展名是.xls。
XSSFWorkbook:一般用于操作Excel2007及以上的版本,扩展名是.xlsx。
XSSFWorkbook wb = new XSSFWorkbook(); //第一步,创建一个webbook,对应一个Excel文件
XSSFSheet sheet=wb.createSheet("sheet1"); //第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
XSSFRow row=sheet.createRow(0); //第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
for (int i = 0; i < items.size(); i++) {
//取数据
DefI nkImportExcleGridDefI = items.get(i);
//创建表头单元格
XSSFCell cell = row.createCell(i);
//表头赋值
cell.setCellValue(nkImportExcleGridDefI.getName());
if (null != nkImportExcleGridDefI.getWidthTemplate()) {
sheet.setColumnWidth(i, nkImportExcleGridDefI.getWidthTemplate() * 256);// 设置第1列单元格列宽,默认1/256字符宽度
}
}
最后使用输出流将文件输出:
FileOutputStream out = new FileOutputStream(new File(finalPath));
wb.write(out);
out.close();
注:解析时如果对象使用不正确会出现以下异常;
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML.
groovy与java相似却又有很多的区别:
在Groovy可以用def定义无类型的变量(定义变量方面def与JavaScript中的var相似),和返回值为无类型的方法,而在Java中没有def