jxl导出excel小demo

1.首先在pom文件加入jar包

<dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
            <type>jar</type>
            <scope>compile</scope>
</dependency>
    

2.然后实操,导出一个文件代码

@Test
    public void test(){
        try  {
            // 打开文件
            WritableWorkbook book = Workbook.createWorkbook( new File( "test.xls" ));
            // 生成名为“第一页”的工作表,参数0表示这是第一页
            WritableSheet sheet = book.createSheet( " 第一页 " , 0 );
            // 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
            // 以及单元格内容为test
            Label label =  new Label( 0 , 0 , " test " );

            // 将定义好的单元格添加到工作表中
            sheet.addCell(label);

            /*
             * 生成一个保存数字的单元格 必须使用Number的完整包路径,否则有语法歧义 单元格位置是第二列,第一行,值为789.123
             */
            jxl.write.Number number =  new jxl.write.Number( 1 , 0 , 555.12541 );
            sheet.addCell(number);


            sheet.setRowView(1,400);

            //字体样式
            WritableFont font = new WritableFont(WritableFont.TAHOMA);
            font.setColour(Colour.BLUE);
            CellFormat cellFormat = new WritableCellFormat(font);
            sheet.addCell(new Label(1,1,"阿拉德大陆",cellFormat));


            // 写入数据并关闭文件
            book.write();
            book.close();

        }  catch (Exception e) {
            System.out.println(e);
        }
    }

会生成一个test.xls文件在项目根目录下,一个例子就完成了!

这是简单案例,其他复杂案例就看具体业务场景了。

原文地址:https://www.cnblogs.com/procedureMonkey/p/10862082.html