gpt4 book ai didi

java - 在 jExcel API 中创建多个工作表

转载 作者:搜寻专家 更新时间:2023-10-31 20:13:34 25 4
gpt4 key购买 nike

如果一张表已满 (65536 行),我需要使用 jExcel API 在 Java 中创建多个 excel 表。假设如果一张纸写满了,那么在下一张纸中它应该从第一张纸停止的地方自动开始书写。我只是坚持将逻辑放在一张纸已满时动态创建它。以下是我到目前为止完成的代码。

public void write() throws IOException, WriteException {
File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();

wbSettings.setLocale(new Locale("en", "EN"));

WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);

writingToExcel(workbook);
}


//Logic to create sheet dyanmically if one is full should be done here I guess?
private void writingToExcel(WritableWorkbook workbook) {

workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
try {
createLabel(excelSheet);
createContent(excelSheet);
} catch (WriteException e) {
e.printStackTrace();
} finally {
try {
workbook.write();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}

}
}

private void createLabel(WritableSheet sheet) throws WriteException {

WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
times = new WritableCellFormat(times10pt);
times.setWrap(true);

WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false,UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
timesBoldUnderline.setWrap(true);

CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);

// Write a few headers
addCaption(sheet, 0, 0, "Header 1");
addCaption(sheet, 1, 0, "This is another header");

}

private void createContent(WritableSheet sheet) throws WriteException,
RowsExceededException {

for (int i = 1; i < 70000; i++) {
addNumber(sheet, 0, i, i + 10);
addNumber(sheet, 1, i, i * i);
}
}

private void addCaption(WritableSheet sheet, int column, int row, String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}

private void addNumber(WritableSheet sheet, int column, int row,
Integer integer) throws WriteException, RowsExceededException {
Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number);
}

我不确定如何在我的代码中添加该逻辑。

有什么建议会有很大帮助吗?

或者无论如何,谁能给我一个简单的例子,如果一张纸已满,它应该自动开始写入另一张纸(第二张纸)?

最佳答案

我对以下两种方法进行了更改:-

private void writingToExcel(WritableWorkbook workbook) {
try {
// don't create a sheet now, instead, pass it in the workbook
createContent(workbook);
}
catch (WriteException e) {
e.printStackTrace();
}
finally {
try {
workbook.write();
workbook.close();
}
catch (IOException e) {
e.printStackTrace();
}
catch (WriteException e) {
e.printStackTrace();
}

}
}

// instead of taking a sheet, take a workbook because we cannot ensure if the sheet can fit all the content at this point
private void createContent(WritableWorkbook workbook) throws WriteException {

int excelSheetIndex = 0;
int rowIndex = 0;
WritableSheet excelSheet = null;

for (int i = 1; i < 70000; i++) {

// if the sheet has hit the cap, then create a new sheet, new label row and reset the row count
if (excelSheet == null || excelSheet.getRows() == 65536) {
excelSheet = workbook.createSheet("Report " + excelSheetIndex, excelSheetIndex++);
createLabel(excelSheet);
rowIndex = 0;
}

// instead of using i for row, use rowIndex
addNumber(excelSheet, 0, rowIndex, i + 10);
addNumber(excelSheet, 1, rowIndex, i * i);

// increment the sheet row
rowIndex++;
}
}

关于java - 在 jExcel API 中创建多个工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14288487/

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