普通操作
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
public static void testExcel2() throws IOException {
HSSFWorkbook workbook=new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(0);
row.createCell(2).setCellValue("aaaaaaaaaaaa");
row.createCell(0).setCellValue(new Date());
workbook.setSheetName(0,"sheet的Name");
FileOutputStream out = new FileOutputStream("D:/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() +".xls");
workbook.write(out);
out.close();
}
public static void main(String[] args) throws IOException {
testExcel2();
}
打包jar上传
public void test(HttpServletRequest request, HttpServletResponse response) {
try {
String agent = request.getHeader("USER-AGENT").toLowerCase();
response.setContentType("application/vnd.ms-excel");
String outFileName = "position";
String filenameSend = "";
if (agent != null && agent.toLowerCase().indexOf("firefox") > 0) {
filenameSend = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(outFileName.getBytes("UTF-8")))) + "?=";
} else {
filenameSend = java.net.URLEncoder.encode(outFileName, "UTF-8");
}
if (agent.contains("firefox")) {
response.setCharacterEncoding("utf-8");
response.setHeader("content-disposition", "attachment;filename=" + filenameSend + ".xlsx");
} else {
response.setHeader("content-disposition", "attachment;filename=" + filenameSend + ".xlsx");
}
ClassPathResource classPathResource = new ClassPathResource("templates/position.xlsx");
InputStream is = classPathResource.getInputStream();
String filePath = classPathResource.getPath();
log.info("filePath---------------" + filePath);
String fileName = classPathResource.getFilename();
log.info("fileName---------------" + fileName);
Workbook wb = ExcelImportUtils.getWorkbook(is, fileName);
is.close();
PositionTypeEnum[] values = PositionTypeEnum.values();
for (int i = 0; i < values.length; i++) {
Sheet sheet = wb.getSheetAt(i);
Row row = sheet.createRow(1);
for (int j = 0; j < 14; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(j + 1);
}
}
OutputStream out = response.getOutputStream();
wb.write(out);
} catch (Exception e) {
log.error("" + e);
}
};
打包jar下载
@ResponseBody
@RequestMapping(value = "/batchImport", method = RequestMethod.POST)
public String batchImportUserKnowledge(@RequestParam(value = "file") MultipartFile file) {
long startMili = System.currentTimeMillis();
if (file == null) {
return ("文件不能为空");
}
long size = file.getSize();
long big = 1024 * 128;
log.info("文件大小为:" + size);
if (size > big) {
return ("文件大小超过128kb,");
}
int filesize = file.getOriginalFilename().length();
String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("\\") + 1, filesize);
log.info("文件名称为:" + fileName);
if (!ExcelImportUtils.validateExcel(fileName)) {
return ("文件类型不正确,请上传excel文件");
}
if (StringUtils.isEmpty(fileName) || size == 0) {
return ("文件内容不可为空");
}
String path = System.getProperty("user.dir");
log.info(path);
String tempUrl = path + "/uploadTemp" + "/";
log.info("______________________________>" + tempUrl);
File uploadDir = new File(tempUrl);
log.info("+++++++++++++>");
if (!uploadDir.exists())
uploadDir.mkdirs();
File tempFile = new File(tempUrl + new Date().getTime() + fileName);
InputStream is = null;
try {
file.transferTo(tempFile);
is = new FileInputStream(tempFile);
Workbook workbook = ExcelImportUtils.getWorkbook(is, fileName);
String sheetName = workbook.getSheetAt(0).getSheetName();
log.info("sheet名称: "+sheetName);
if (tempFile.exists()) {
tempFile.delete();
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
is.close();
} catch (IOException e) {
log.error("报错+++++++++++++++++++++++报错");
e.printStackTrace();
}
}
long endMili = System.currentTimeMillis();
log.info("excel批量导入总耗时为:" + (endMili - startMili) + "毫秒");
return ("导入成功");
}