`
qinjingkai
  • 浏览: 259815 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

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)在附件中

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics