talha06
talha06

Reputation: 6466

jxl - MS Excel compatibility error

I'm trying to generate an Excel report using jxl API - but I have been facing compatibility issues with MS Excel. The generated report which contains some styles such as bold fonts, auto resized cells, etc. works like a charm with LibreOffice Calc. But if I try to open the same .xls file with MS Excel, I face with a warning like this:

screenshot

When I confirm MS Excel prompt to continue to recover the file, I am able to see the sheet content without alignments and styles.

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?

Here is a code snippet from the service that generates the report:

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

Upvotes: 1

Views: 665

Answers (1)

Ya Wang
Ya Wang

Reputation: 1808

Sorry, I am not a jxl api professional. But I do want to suggest the usage of difference API for your windows office modification needs.

http://poi.apache.org/ Apache POI to my knowledge is more stable and supported official API.

Upvotes: 1

Related Questions