在jxl中写入Excel

时间:2017-02-09 09:54:30

标签: java excel

这些是导入

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

代码在第一行导入java.io时抛出错误 但导入后我得到另一个错误“使用try catch”

WritableWorkbook workbook =Workbook.createWorkbook(new File("C:\\IronMan.xls"));
    WritableSheet sheet=workbook.createSheet("Tony", 0);
    Label label=new Label(0,0,"Real Name");
    sheet.addCell(label);
    Label label1=new Label(1,0,"Character Name");
    sheet.addCell(label1);
    Label label2=new Label(2,0,"Ability");
    sheet.addCell(label2);
    workbook.write();
    workbook.close();

1 个答案:

答案 0 :(得分:1)

我在这里没有看到问题,因为我们正在处理文件IO,但有时会出现异常。您可以调整以下代码,它应该适合您。

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class MyClass {

    public static void main(String[] args) {
        WritableWorkbook workbook;
        try {
            workbook = Workbook.createWorkbook(new File("C:\\IronMan.xls"));
            WritableSheet sheet = workbook.createSheet("Tony", 0);
            Label label = new Label(0, 0, "Real Name");
            sheet.addCell(label);
            Label label1 = new Label(1, 0, "Character Name");
            sheet.addCell(label1);
            Label label2 = new Label(2, 0, "Ability");
            sheet.addCell(label2);
            workbook.write();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}