0

我用jxl写法如下:

WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));

WritableSheet sheet = workbook.createSheet("First Sheet", 0);

Label label = new Label(0, 2, "A label record"); 
sheet.addCell(label); 

Number number = new Number(3, 4, 3.1459); 
sheet.addCell(number);

当我尝试打开文件时,出现错误:

您尝试打开的文件的格式不同于扩展名指定的格式。在打开文件之前验证文件没有损坏并且来自受信任的来源。您现在要打开文件吗?

当我单击是时,它是空的。

4

2 回答 2

3

您需要添加以下语句:

workbook.write(); 
workbook.close();
于 2013-06-11T04:25:12.273 回答
0

我不能说你的代码,但如果你想将数据写入 excel 文件,那么你可以使用这个特定的代码,我已经使用了 1000 多次,

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.Formula;
import jxl.write.Label;
import jxl.write.Number;
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;
    private String[][] contentD;
    private int value;

    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);

        // Write a few headers
        addCaption(sheet, 0, 1, "Images");
        addCaption(sheet, 2, 1, "XXXXXXXX");


    }

    private void createContent(WritableSheet sheet) throws WriteException,
            RowsExceededException {
        // Lets calculate the sum of it
        StringBuffer buf = new StringBuffer();
        buf.append("SUM(A2:A10)");
        Formula f = new Formula(0, 10, buf.toString());
        sheet.addCell(f);
        buf = new StringBuffer();
        buf.append("SUM(B2:B10)");
        f = new Formula(1, 10, buf.toString());
        sheet.addCell(f);

        // Now a bit of text
        for (int i = 0; i < contentD.length; i++) {
            for (int j = 0; j < contentD[0].length; j++) {
                // First column
                addLabel(sheet, j, i + 2, contentD[i][j]);
            }
        }
    }

    private void addCaption(WritableSheet sheet, int column, int row, String s)
            throws RowsExceededException, WriteException {
        Label label;
        label = new Label(column, row, s, timesBoldUnderline);
        sheet.addCell(label);
    }

    private void addNumber(WritableSheet sheet, int column, int row,
            Integer integer) throws WriteException, RowsExceededException {
        Number number;
        number = new Number(column, row, integer, times);
        sheet.addCell(number);
    }

    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 WriteExcel(String fileName, String[][] content) throws WriteException, IOException {
        contentD = content;
        setOutputFile(fileName);
        write();
        System.out.println("Please check the result file under c:/temp/lars.xls ");
    }
}

就这样称呼吧

WriteExcel writeExcel = new WriteExcel("d:\res.xls", contentData);

其中 contentData 是一个二维数组,其中包含 Excel 工作表的数据。

于 2013-06-11T04:33:41.000 回答