gpt4 book ai didi

java - jxl - MS Excel 兼容性错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:17:34 25 4
gpt4 key购买 nike

我正在尝试使用 jxl API 生成 Excel 报告 - 但我一直面临与 MS Excel 的兼容性问题。生成的报告包含一些样式,例如粗体字体、自动调整单元格大小等,就像 LibreOffice Calc 的魅力一样。但是,如果我尝试使用 MS Excel 打开相同的 .xls 文件,我会遇到如下警告:

screenshot

当我确认 MS Excel 提示继续恢复文件时,我能够看到工作表内容没有对齐和样式

Edit: Just figured out; merging cells is the reason of this issue. If I do not merge cells, everything works as it is expected. So how can I merge cells that is compatible with MS Excel using jxl?

以下是生成报告的服务的代码片段:

WritableFont boldArial11Font = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD);
WritableFont arial11Font = new WritableFont(WritableFont.ARIAL, 11);

CellView sbtSizeCellView = new CellView();
sbtSizeCellView.setSize(4000);
sbtSizeCellView.setAutosize(false);

CellView autoSizeCellView = new CellView();
autoSizeCellView.setAutosize(true);

WritableCellFormat boldArial10Format = new WritableCellFormat(boldArial11Font);
boldArial10Format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
WritableCellFormat boldArial10CenterFormat = new WritableCellFormat(boldArial11Font);
boldArial10CenterFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
boldArial10CenterFormat.setAlignment(Alignment.CENTRE);
WritableCellFormat arial10Format = new WritableCellFormat(arial11Font);
arial10Format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);

WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("tr", "TR"));

WritableWorkbook workbook = Workbook.createWorkbook(baos, wbSettings);
WritableSheet sheet = workbook.createSheet(documentName, 0);
sheet.setPageSetup(PageOrientation.PORTRAIT, PaperSize.A4, 0, 0);

String upTitle = Constants.REPORT_UP_TITLE;
String title = "Report";

sheet.mergeCells(0, 0, 2, 0); // REASON OF THIS ISSUE!!
Label header = new Label(0, 0, upTitle, boldArial10CenterFormat);
sheet.addCell(header);

sheet.mergeCells(0, 1, 2, 0); // REASON OF THIS ISSUE!!
header = new Label(0, 1, title, boldArial10CenterFormat);
sheet.addCell(header);

// HEADERS
int i = 0;
int satir = 2;
Label label = new Label(i, satir, "#", boldArial10Format);
sheet.addCell(label);
sheet.setColumnView(i, sbtSizeCellView);

i++;
label = new Label(i, satir, "Column 1", boldArial10Format);
sheet.addCell(label);
sheet.setColumnView(i, autoSizeCellView);

i++;
label = new Label(i, satir, "Column 2", boldArial10Format);
sheet.addCell(label);
sheet.setColumnView(i, autoSizeCellView);

for (int j = 0; j < list.size(); j++) {
int sutun = 0;
Label dataLabel = new Label(sutun, j + 3, (j + 1) + "", arial10Format);
sheet.addCell(dataLabel);

sutun++;
dataLabel = new Label(sutun, j + 3, "Row 1", arial10Format);
sheet.addCell(dataLabel);

sutun++;
dataLabel = new Label(sutun, j + 3, "Row 1", arial10Format);
sheet.addCell(dataLabel);
}

workbook.write();
workbook.close();

最佳答案

抱歉,我不是 jxl api 专业人士。但我确实想建议您使用不同的 API 来满足您的 Windows Office 修改需求。

http://poi.apache.org/据我所知,Apache POI 更稳定并且支持官方 API。

关于java - jxl - MS Excel 兼容性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27892397/

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