3

我试过使用Jexcel

如 Vogella教程中所述,更新现有的 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);

这是在指定的列和行添加单元格,但会清除其余的 excel 数据。

如何将数据添加到保留数据的现有 Excel 中?

以下是程序(参考:Vogella)。Excel 表已经有 20 行和前 2 列的数据,我正在尝试在第 3 列添加 20 行包 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 ");
    }
}
4

1 回答 1

0

您正在使用WritableWorkbook它创建一个新的可写工作簿,而不是编写前一个。要修改现有工作簿,您首先必须创建现有工作簿的副本,如下所示:

Workbook workbook = Workbook.getWorkbook(new File("myfile.xls")); 
WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"), workbook);

然后在副本上工作。有关更多详细信息,请参阅(提取 api 的路径)\jexcelapi\src\jxl\demo\ReadWrite.java 或导航到此页面的复制和修改电子表格。

于 2013-07-08T15:26:15.727 回答