文件数据可能已经失去了excel Apache的poi

这个问题以前曾经问过,但是我的问题是完全不同的。 我在两台不同的机器上运行我的程序,包括mac和使用Microsoft Excel mac版本来打开Excel文件。 奇怪的是一台机器完美工作,另一台机器给我一个“文件数据可能已经丢失”。 我从我的git帐户拉的程序,并确认这是最新的版本,我清除我的caching在我的浏览器,但它仍然给我同样的错误之前,显示一个空白的Excel工作表。

JobOrderGenerator

public class JobOrderGenerator { private OutputStream out; private int sheetNumber = 0; private HSSFWorkbook workbook; private HSSFSheet sheet; @Inject HttpServletResponse response; @Inject GenerateTemplate generateTemplate; public JobOrderGenerator(List<ShopOrder> shopOrder, HttpServletResponse response) throws InvalidFormatException, IOException { this.response = response; createWorkBook(); createJobOrder(shopOrder); createFile(); } private void createWorkBook() { workbook = new HSSFWorkbook(); } private void createSheet(){ //String safeName = WorkbookUtil.createSafeSheetName(sheetName); sheet = workbook.createSheet(); generatShopOrderTemplate(); } private void generatShopOrderTemplate(){ generateTemplate = new GenerateTemplate(); generateTemplate.applyTemplate(workbook, sheet); } private void createJobOrder(List<ShopOrder> order) throws InvalidFormatException, IOException { //get current date time with Date() Date todaysDate = new Date(); for(ShopOrder shopOrder: order){ writeToSpecificCell(1, 1, sheetNumber, shopOrder.getPo_number()); // Po Number writeToSpecificCell(6, 3, sheetNumber, shopOrder.getPart_number()); // Part Number LocalDate date = shopOrder.getPo_due_date(); String dateToString = date.toString(); writeToSpecificCell(5, 7, sheetNumber, dateToString); // Due_Date writeToSpecificCell(2, 1, sheetNumber, todaysDate.toString());//todays date writeToSpecificCell(6, 7, sheetNumber, Integer.toString(shopOrder.getPart_quantity())); // Part Quantity //writeToSpecificCell(1,2,sheetNumber, shopOrder.getMaterial); //Material writeToSpecificCell(7, 3, sheetNumber, shopOrder.getPart_decription()); // Part Description writeToSpecificCell(5,3,sheetNumber, shopOrder.getCustomer_name()); //Customer writeToSpecificCell(10, 1, sheetNumber, shopOrder.getMachine_number()); // Machine writeToSpecificCell(7, 7, sheetNumber, shopOrder.getMaterial_number()); // Material Number sheetNumber++; } } private void writeToSpecificCell(int rowNumber, int cellNumber, int sheetNumber, String value) throws InvalidFormatException { createSheet(); try { sheet = workbook.getSheetAt(sheetNumber); HSSFRow row = sheet.getRow(rowNumber); HSSFCell cell = row.createCell(cellNumber); if (cell == null) { cell = row.createCell(cellNumber); } cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(value); } catch (NullPointerException ex) { System.out.println("writeToSpecificCell class is returning null "); ex.getStackTrace(); } } private void createFile(){ // Set Excel File Name String fileName = "JobTicket.xls"; // Set HTT response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); try { out = response.getOutputStream(); workbook.write(out); out.flush(); } catch (IOException e) { e.printStackTrace(); } } } 

模板创build者

 @Named public class GenerateTemplate { public HSSFWorkbook applyTemplate(HSSFWorkbook workbook, HSSFSheet sheet) { //total rows to create (starts at 0) int totalRows = 26;//27 total rows sheet.setColumnWidth(1, 4300); sheet.setColumnWidth(8, 3000); //create sheet rows for(int i = 0; i < totalRows; i++) { sheet.createRow(i); } //Fonts //Bold Header Font HSSFFont boldHeaderFont = workbook.createFont(); boldHeaderFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); boldHeaderFont.setFontName("Broadway"); boldHeaderFont.setItalic(true); boldHeaderFont.setFontHeightInPoints((short) 16); //Default Font HSSFFont defaultFont = workbook.createFont(); defaultFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); //Bold Font HSSFFont boldFont = workbook.createFont(); boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //Bold blue Font HSSFFont boldBlueFont = workbook.createFont(); boldBlueFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); boldBlueFont.setColor(HSSFColor.BLUE.index); //Bold blue Tall Font HSSFFont boldBlueTallFont = workbook.createFont(); boldBlueTallFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); boldBlueTallFont.setColor(HSSFColor.BLUE.index); boldBlueTallFont.setFontHeightInPoints((short) 14); //Styles //Bold Header HSSFCellStyle boldHeaderStyle = workbook.createCellStyle(); boldHeaderStyle.setFont(boldHeaderFont); boldHeaderStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //Center HSSFCellStyle centerStyle = workbook.createCellStyle(); centerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //Bold HSSFCellStyle boldStyle = workbook.createCellStyle(); boldStyle.setFont(boldFont); //Grey Background fill HSSFCellStyle greyFillStyle = workbook.createCellStyle(); greyFillStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); greyFillStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); //Centered Bold Blue Style HSSFCellStyle centeredBoldBlueStyle = workbook.createCellStyle(); centeredBoldBlueStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); centeredBoldBlueStyle.setFont(boldBlueFont); //Centered Bold Blue Tall Style HSSFCellStyle centeredBoldBlueTallStyle = workbook.createCellStyle(); centeredBoldBlueTallStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); centeredBoldBlueTallStyle.setFont(boldBlueTallFont); //Merged Cells //Name and Address merged sheet.addMergedRegion(new CellRangeAddress(0,0,2,8)); sheet.addMergedRegion(new CellRangeAddress(1,1,2,8)); sheet.addMergedRegion(new CellRangeAddress(2,2,2,8)); sheet.addMergedRegion(new CellRangeAddress(3,3,2,8)); sheet.addMergedRegion(new CellRangeAddress(4,4,2,8)); //Customer merged sheet.addMergedRegion(new CellRangeAddress(5,5,3,5)); //Part Merged sheet.addMergedRegion(new CellRangeAddress(6,6,3,5)); //Create template cells //column 1 sheet.createRow(0).createCell(0).setCellValue("Job No."); sheet.createRow(1).createCell(0).setCellValue("PO No."); sheet.createRow(2).createCell(0).setCellValue("Date"); HSSFRow row; HSSFCell cell; //Name and Address Info row = sheet.getRow(1); cell = row.createCell(2); cell.setCellValue("Hillcrest Tool & Die"); cell.setCellStyle(boldHeaderStyle); row = sheet.getRow(2); cell = row.createCell(2); cell.setCellValue("807 Jones Rd, Paragould AR"); cell.setCellStyle(centerStyle); row = sheet.getRow(3); cell = row.createCell(2); cell.setCellValue("(870)573-6881"); cell.setCellStyle(centerStyle); //Grey Background Cells row = sheet.getRow(0); cell = row.createCell(2); cell.setCellStyle(greyFillStyle); row = sheet.getRow(4); cell = row.createCell(2); cell.setCellStyle(greyFillStyle); row = sheet.getRow(8); cell = row.createCell(0); cell.setCellStyle(greyFillStyle); row = sheet.getRow(8); cell = row.createCell(1); cell.setCellStyle(greyFillStyle); //Customer centered row = sheet.getRow(5); cell = row.createCell(3); cell.setCellStyle(centeredBoldBlueStyle); //part centered row = sheet.getRow(6); cell = row.createCell(3); cell.setCellStyle(centeredBoldBlueTallStyle); sheet.getRow(5).createCell(2).setCellValue("Customer"); sheet.getRow(5).createCell(6).setCellValue("Due Date"); sheet.getRow(6).createCell(2).setCellValue("Part No."); sheet.getRow(6).createCell(6).setCellValue("Quantity"); sheet.createRow(7).createCell(0).setCellValue("PARTIAL"); sheet.getRow(7).createCell(2).setCellValue("Part Description"); sheet.getRow(7).createCell(6).setCellValue("Material"); sheet.getRow(9).createCell(3).setCellValue("Initials"); sheet.getRow(9).createCell(4).setCellValue("Date"); sheet.getRow(9).createCell(5).setCellValue("Operation"); sheet.getRow(9).createCell(6).setCellValue("Qty"); sheet.getRow(9).createCell(7).setCellValue("Total"); sheet.getRow(9).createCell(8).setCellValue("Hours"); sheet.createRow(10).createCell(0).setCellValue("Machine"); sheet.getRow(10).createCell(1).setCellValue("LASER / PLASMA"); sheet.getRow(17).createCell(1).setCellValue("BEND / MILL"); return workbook; } }