我試圖用JAVA Excel API vogella修改現有的excel文件,但無法處理它。我找到了重新創建excel文件的方法,但這不是我想要的。有人可以提供建議,提示或分享一個簡短的例子。使用基於Java的API讀取/寫入Excel - vogella
我分享我想要做的事:
/**
* Reading external file.
*
* @return Record[];
* @throws IOException;
* @throws BiffException.
*/
public Record[] read() throws IOException {
File inputWorkbook = new File(inputFile);
Workbook w;
Record[] records = null;
int rowsCount = 0;
try {
w = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = w.getSheet(0);
int columnsCount = sheet.getColumns();
rowsCount = sheet.getRows();
records = new Record[rowsCount];
for (int rowIndex = 0; rowIndex < rowsCount; rowIndex++) {
Record record = new Record(columnsCount);
for (int columnIndex = 0; columnIndex < columnsCount; columnIndex++) {
Cell cell = sheet.getCell(columnIndex, rowIndex);
String value = cell.getContents();
record.setRecordData(columnIndex, value);
}
records[rowIndex] = record;
}
} catch (BiffException e) {
errorMessage = 1;
}
return records;
}
/**
* Writing to existing excel file.
*
* @param records;
* @throws IOException;
* @throws WriteException;
* @throws BiffException.
*/
public void write(Record[] records) throws IOException, WriteException, BiffException {
File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
Workbook workbook = Workbook.getWorkbook(file, wbSettings);
WritableWorkbook writableworkbook = Workbook.createWorkbook(file, workbook, wbSettings);
WritableSheet excelSheet = writableworkbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet, records);
writableworkbook.write();
writableworkbook.close();
}
通過更具體的方式,我們可以更容易地弄清楚自己想要什麼。發佈你到目前爲止所做的事情,舉例說明你爲什麼得到的不是你想要的。沒有具體,這不是一個真正的問題。 – beny23 2012-03-23 12:18:09
@ beny23:我想將額外的自定義數據插入到現有的Excel文件中,例如插入到第一個工作表中。 – nenito 2012-03-23 12:22:18
需要詳細說明'無法處理' – 2012-03-23 12:24:59