java – 使用jxl将数据添加到Excel文件

我正在使用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,1,0);
        sheet.mergeCells(3,4,0);
        sheet.mergeCells(6,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文件,仅包含合并的单元格.

如何介绍修改

解决方法

既然你正在操作一个不是w2类的成员的WritableWorkbook对象,你不应该让你的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,2);
    return sheet;
    }

基本上.对addNumber的类似修改似乎也是如此.然后相应的调用将是sheet = addNumber(sheet,2);和w2 =修改(w2);

相关文章

ArrayList简介:ArrayList 的底层是数组队列,相当于动态数组。与 Java 中的数组相比,它的容量能动态增...
一、进程与线程 进程:是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位。 线程...
本文为博客园作者所写: 一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 简单的一个类...
#############java面向对象详解#############1、面向对象基本概念2、类与对象3、类和对象的定义格式4、...
一、什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错。在java中,阻止当前方法或作用域...
Collection接口 Collection接口 Collection接口 Collection是最基本的集合接口,一个Collection代表一组...