知识库

推荐内容

快逸做的更好

产品优势

您的位置: 首页 > 知识库 > 设计基础 > 快逸报表中将Excel作为数据源

快逸报表中将Excel作为数据源

快逸报表可以使用自己的Excel作为数据源导入数据,这个操作需要自定义类,具体类的写法是:

 

 

package com.runqian.api;

 

import java.io.FileInputStream;

import java.io.IOException;

 

import org.apache.poi2.hssf.usermodel.HSSFCell;

import org.apache.poi2.hssf.usermodel.HSSFRow;

import org.apache.poi2.hssf.usermodel.HSSFSheet;

import org.apache.poi2.hssf.usermodel.HSSFWorkbook;

 

import com.runqian.report4.dataset.DataSet;

import com.runqian.report4.dataset.IDataSetFactory;

import com.runqian.report4.dataset.Row;

import com.runqian.report4.usermodel.Context;

import com.runqian.report4.usermodel.CustomDataSetConfig;

import com.runqian.report4.usermodel.DataSetConfig;

 

public class ExcelDataSetFactory implements IDataSetFactory {

       //本方法为IDataSetFactory中的方法,由系统调用

       public DataSet createDataSet(Context context, DataSetConfig dsc, boolean arg2) {

              String fileName = null;

              int sheetNum = 0;

              FileInputStream is = null;

              HSSFWorkbook wb = null;

 

              CustomDataSetConfig cdsc = (CustomDataSetConfig)dsc;

              String args[] = cdsc.getArgNames();    // 取参数名数组

              String values[] = cdsc.getArgValue();   // 取参数值数组

              for(int i=0;i<args.length;i++){

                     if(args[i].equals(“fileName”)) fileName = values[i];

                     if(args[i].equals(“sheetNum”)) sheetNum = Integer.parseInt(values[i]);

              }

              try {

                     is = new FileInputStream(fileName);

                     wb = new HSSFWorkbook(is);

              } catch (IOException e) {

                     e.printStackTrace();

              }

             

              DataSet ds = new DataSet(“ds1″);

              HSSFSheet hs = wb.getSheetAt( sheetNum ); //获得EXCEL的工作表

              HSSFRow hr = hs.getRow(0);

              HSSFCell hc = null;

             

              //EXCEL文件里获得数据集的列名

              int rowCount = hs.getLastRowNum() + 1;

              int colCount = hr.getLastCellNum();

              //根据Excel的列数据,生成对应的数据集列数

              for(int i = 0;i <= colCount – 1; i++){

                     hc = hr.getCell((short)i);

                     String s1 = hc.getStringCellValue();

                     ds.addCol(s1);

              }

              //获得数据集数据,从第二行开始

              for(int j = 1;j < rowCount;j++){

                     Row row = ds.addRow();

                     //根据Excel中的数据对数据集每一行进行设置数据

                     for(int k = 1;k <= colCount;k++){

                            hr = hs.getRow(j);

                            hc = hr.getCell((short)(k – 1));

                            if(hc.getCellType() == HSSFCell.CELL_TYPE_STRING){

                                   row.setData(k,hc.getStringCellValue());

                            }else if(hc.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){

                                   row.setData(k,new Double(hc.getNumericCellValue()));

                            }

                     }

              }

              return ds;

       }

}

发布日期:2008/08/06
本文分类: 设计基础