欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > java-使用HSSFWorkbook编辑excel文件

java-使用HSSFWorkbook编辑excel文件

2024/11/30 13:13:00 来源:https://blog.csdn.net/U12Euphoria/article/details/144056694  浏览:    关键词:java-使用HSSFWorkbook编辑excel文件

HSSF基本操作
HSSF表格案例

对模板excel进行修改然后导出,返回给前端文件路径。

	public Result createProformaInvoiceXls(Map<String, Object> params, HttpServletRequest request) {Result result = null; // 只设置几个基本的信息到result上更新前台try {String tempFilePath = ExportPathConstants.EXCEL_PATH + File.separator + "proformaInvoiceTemp" + File.separator + "proformaInvoice.xls";final String generate_XLSFile_Dir = ExportPathConstants.EXCEL_PATH + File.separator + "ExportTempFileDir" + File.separator + "ExportTempXLSFileDir"+ File.separator;// 注意路径和sheet名时不要用特殊字符final String fileName = "ProformaInvoice__" + SapDateFormat.formatTimeStamp(new Date()) + ".xls";String fileSuffix = ".tmp";final String tempFile = fileName + fileSuffix;new Thread(() -> {try {List<Map<String, Object>> lrecns = (List<Map<String, Object>>)params.get("LRECNS");ExportData exportData = getInvoiceInfo("PT_ZLRECD", lrecns);File file = ExportFileUtil.getFile(generate_XLSFile_Dir, tempFile);OutputStream fos = new FileOutputStream(file);if(writePIXlsFile(exportData, fos, file, tempFilePath)) {if (file.isFile()) {file.renameTo(new File(generate_XLSFile_Dir + fileName));}}} catch (Exception e) {e.printStackTrace();}}).start();result =  new Result(Message.SUCCESS, new String[]{generate_XLSFile_Dir, fileName});} catch (Exception e) {e.printStackTrace();result = new Result(Message.FAILED, Message.PLEASE_CONTACT_ADMINISTRATOR + e.getMessage());}return result;}
	private boolean writePIXlsFile(ExportData exportData, OutputStream fos, File newFile, String tempFilePath) throws Exception{newFile = copyNewFile(fos, newFile, tempFilePath);InputStream is = null;HSSFWorkbook workbook = null;HSSFSheet sheet = null;is = new FileInputStream(newFile);workbook = new HSSFWorkbook(is);sheet = workbook.getSheet("PI");//创建样式HSSFCellStyle boderStyle = workbook.createCellStyle();boderStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);boderStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);boderStyle.setBorderBottom(CellStyle.BORDER_THIN); boderStyle.setBorderLeft(CellStyle.BORDER_THIN);// 左边框boderStyle.setBorderTop(CellStyle.BORDER_THIN);// 上边框boderStyle.setBorderRight(CellStyle.BORDER_THIN);// 右边框//创建明细HSSFRow dataRow = null;HSSFCell dataCell = null;Date now = new Date();sheet = workbook.getSheet("PI");dataRow = sheet.getRow(7);dataCell = dataRow.getCell(0);dataCell.setCellValue("B" + SapDateFormat.formatDate(now) + "A");workbook.write(fos);try {fos.close();} catch (Exception e) {}return true;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com