1.将MultpartFile(Spring)转化为InputStream
/**
* 转化为InputStream
*
* @param multipartFile multipartFile
* @return InputStream
* @throws IOException IOException
*/
public static InputStream parseInputStream(MultipartFile multipartFile) throws IOException {
File file = File.createTempFile("temp", null);
multipartFile.transferTo(file);
FileInputStream inputStream = new FileInputStream(file);
file.deleteOnExit();
return inputStream;
}
2.将文件InputStream转化为Workbook
/**
* 获取Workbook
*
* @param inputStream inputStream
* @return inputStream
* @throws IOException IOException
*/
public static Workbook getWorkBook(InputStream inputStream) throws IOException {
return WorkbookFactory.create(inputStream);
}
3.设置值以及获取值
public static boolean checkExcelFormat(Workbook wb) {
int count = wb.getNumberOfSheets();
Sheet sheet = sheet = wb.getSheetAt(0);
Row row = sheet.getRow(0);
// 获取A1的String的值
row.getCell(0).getStringCellValue();
// 设置A1的值
row.getCell(0).setCellValue("value");
}
4.设置一列为文本类型
// i为列数, textCellStyle(wb)为表格样式
sheet.setDefaultColumnStyle(i, textCellStyle(wb));
5.设置样式
private static CellStyle textCellStyle(Workbook wb) {
CellStyle textCellStyle = wb.createCellStyle();
textCellStyle.setAlignment(HorizontalAlignment.CENTER);
DataFormat format = wb.createDataFormat();
textCellStyle.setDataFormat(format.getFormat("@"));
return textCellStyle;
}