如Vogella tutorial討論更新現有的Excel工作表嘗試數據。
這裏的問題是已有的數據已經存在於現有的Excel表格中,並且被新編寫的excel數據湮沒了。
例如,如果我在Excel中
<table>
<tr>
<td>1</td> <td>2 </td>
</tr>
<tr>
<td>3</td> <td>4 </td>
</tr>
<table>
已經這一點,我希望寫程序執行後,將數據添加到旁邊的2和4的新的細胞,像
<table>
<tr>
<td>1</td> <td>2 </td> <td>X </td>
</tr>
<tr>
<td>3</td> <td>4 </td> <td>Y </td>
</tr>
<table>
這是我得到的
<table>
<tr>
<td> </td> <td> </td> <td>X </td>
</tr>
<tr>
<td> </td> <td> </td> <td>Y </td>
</tr>
<table>
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
這是添加指定的列和行的單元格,但擦除r最好的數據。
如何將數據添加到保存數據的現有excel中?
以下是該程序(參考:Vogella)。 Excel工作表已經有20行數據和 前2列,我試圖在第3列添加20行數據 package excel;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class WriteExcel {
private WritableCellFormat timesBoldUnderline;
private WritableCellFormat times;
private String inputFile;
public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
}
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);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close();
}
private void createLabel(WritableSheet sheet) throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true);
// Create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(
WritableFont.TIMES, 10, WritableFont.BOLD, false,
UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);
CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);
}
private void createContent(WritableSheet sheet) throws WriteException,
RowsExceededException {
Integer salary = 1000;
// Now a bit of text
for (int i = 0; i < 20; i++) {
// third column
addLabel(sheet, 2, i, salary.toString());
// WritableCell cell = sheet.getWritableCell(2, i);
// if (cell.getType() == CellType.LABEL) {
// Label l = (Label) cell;
// l.setString("modified cell");
// }
salary += 1000;
}
}
private void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
}
public static void main(String[] args) throws WriteException, IOException {
WriteExcel test = new WriteExcel();
test.setOutputFile("c:/temp/lars.xls");
test.write();
System.out
.println("Please check the result file under c:/temp/lars.xls ");
}
}
另一個框架[DataFile](http://datafile.sourceforge.net/)在DataRow上似乎沒有任何API來在特定的單元格(行,列)上添加數據。 – user2527039
您用於創建工作簿的代碼是什麼? –
添加了上面的代碼。請檢查。 – user2527039