gpt4 book ai didi

java - 文件数据可能已丢失 excel apache poi

转载 作者:行者123 更新时间:2023-12-04 21:03:21 25 4
gpt4 key购买 nike

这个问题之前已经被问过,但我的问题肯定是不同的。我在两台不同的机器上运行我的程序,都是 mac 并使用 Microsoft Excel mac 版本打开 Excel 文件。奇怪的是,一台机器运行良好,另一台给我一个“文件数据可能已丢失”。我从我的 git 帐户中提取了该程序并确认它是最新版本,并且我在浏览器中清除了我的缓存,但在显示空白的 Excel 工作表之前它仍然给我同样的错误。

工作订单生成器

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();
}

}

}

模板创建者
@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("P.O. 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;
}

}

最佳答案

更改所有内容HSSF工作簿 XSSF 工作簿 .因为 HSSF工作簿对象创建 XLS 它不支持生成负值,而且它很旧,所以使用 XSSF 工作簿 创建 XLSX .

关于java - 文件数据可能已丢失 excel apache poi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31244067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com