java之报表

  利用jxl包中的类来制作报表代码大致如下:

package report;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.LabelCell;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
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;
import entity.Student;

public class ReportTools {
   
   
    public void createExcel(List dataList){
        String path="d:/temp/temp.xls";
        File file=new File(path);
        try {
            WritableWorkbook wb=Workbook.createWorkbook(file);
            //WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
            WritableSheet sheet=wb.createSheet("TestCreateExcel", 0);
            Label label;
           
            WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.BLUE);
            WritableCellFormat headerFormat = new WritableCellFormat(headerFont);

           
            WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10,
                    WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.BLACK);
            WritableCellFormat detFormat = new WritableCellFormat(detFont);

           
            label=new Label(0,0,"学生列表",headerFormat);
           
           
            sheet.addCell(label);

            int column=0;
            label=new Label(column++,2,"id号",headerFormat);
            sheet.addCell(label);
            label=new Label(column++,2,"名字",headerFormat);
            sheet.addCell(label);
           
            for(int i=0;i<dataList.size();i++){
                Student stu=(Student) dataList.get(i);
                int cloumn=0;
                label=new Label(cloumn++,i+3,stu.getStuId().toString(),detFormat);
                sheet.addCell(label);
                label=new Label(cloumn++,i+3,stu.getName(),detFormat);
                sheet.addCell(label);
               
               
            }
           
           
            wb.write();
            wb.close();
           
           
           
           
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
       
       
       
       
       
       
       
       
       
    }
   
    public static void main(String[] args) {
        Student s1=new Student();
        s1.setStuId(new Integer(1));
        s1.setName("101");
       
        Student s2=new Student();
        s2.setStuId(new Integer(2));
        s2.setName("2222");
       
        List data=new ArrayList();
        data.add(s1);
        data.add(s2);
       
         new ReportTools().createExcel(data);
    }

}
所需的jar包(jxl)在附件中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值