簡體   English   中英

使用 jxl 將數據添加到 Excel 文件

[英]Adding data to an Excel file using jxl

我正在使用 jxl 創建一個 excel 文件。 首先應該只包含一些關於樣式的格式和信息。 然后,每次有人向其中添加新數據時都應該更新它。

public class WriteExcel {

    private static WritableWorkbook workbook;
    private static WritableCellFormat timesStandard;
    private String inputFile;
    final private static int FONT_SIZE = 12;


    public void setOutputFile(String inputFile) {
        this.inputFile = inputFile;
    }

    private void prepareSheet(WritableSheet sheet) throws WriteException {
        sheet.mergeCells(0, 0, 1, 0);
        sheet.mergeCells(3, 0, 4, 0);
        sheet.mergeCells(6, 0, 7, 0);

        WritableFont times12pt = new WritableFont(WritableFont.TIMES, FONT_SIZE);
        timesStandard = new WritableCellFormat(times12pt);

        CellView cv = new CellView();
        cv.setFormat(timesStandard);
    }

    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("First", 0); 
        WritableSheet excelSheet = workbook.getSheet(0);
        prepareSheet(excelSheet);

        workbook.write();
        workbook.close();
    }

    public static void main(String[] args) throws WriteException, IOException {
        WriteExcel test = new WriteExcel();
        test.setOutputFile("c:/Users/H/Desktop/test.xls");
        test.write();
    }
}

我知道我需要另一個類來讓我訪問文件並向其中添加一些數據:

class Modify {

    private static Logger logger = Logger.getLogger(Modify.class);
    private File inputWorkbook;
    private File outputWorkbook;

    public Modify(String input, String output) {
        inputWorkbook = new File(input);
        outputWorkbook = new File(output);
        logger.info("Input file:  " + input);
        logger.info("Output file:  " + output);
    }

    public void readWrite() throws IOException, BiffException, WriteException {
        logger.info("Reading...");
        Workbook w1 = Workbook.getWorkbook(inputWorkbook);

        logger.info("Copying...");
        WritableWorkbook w2 = Workbook.createWorkbook(outputWorkbook, w1);

        if (inputWorkbook.getName().equals("test.xls")) {
            modify(w2);
        }

        w2.write();
        w2.close();
        logger.info("Done");
    }

    private void modify(WritableWorkbook w) throws WriteException {
        logger.info("Modifying...");

        WritableSheet sheet = w.getSheet("First");

        //createContent(sheet); // contains methods responsible for adding data, for example:
        addNumber(sheet, cols, rows, 2);

    private static void addNumber(WritableSheet sheet, int column, int row, double d) throws WriteException, RowsExceededException {
        Number number;
        number = new Number(column, row, d, timesStandard);
        sheet.addCell(number);

    }
}

但我最終得到一個只有合並單元格的空白 Excel 文件。

我如何引入修改?

由於您正在操作一個WritableWorkbook對象,該對象不是您的w2類的成員,您不應該讓modify()方法不為 void 而是返回WritableWorkbook嗎?

在我看來,當您調用modify()時會實例化一個新的,但由於范圍的原因,最后的所有更改都將被刪除。

最后你會得到類似的東西

    private WritableWorkbook modify(WritableWorkbook w) throws WriteException {
    logger.info("Modifying...");

    WritableSheet sheet = w.getSheet("First");

    //createContent(sheet); // contains methods responsible for adding data, for example:
    addNumber(sheet, cols, rows, 2);
    return sheet;
    }

很基本。 對 addNumber 的類似修改似乎也符合要求。 然后相應的調用將是sheet = addNumber(sheet, cols, rows, 2); w2 = modify(w2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM