报表知识库
我要提问

行式填报表导入excel数据直接入库

由于行式填报在导入excel的数据量比较大的时候,页面加载数据会很慢,在这种情况下可以采取后台直接导入数据库的方法,实例如下:

package example;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Locale;

import com.runqian.base4.resources.FormatUtils;
import com.runqian.report4.ide.ExcelImporter;
import com.runqian.report4.input.DataSaver;
import com.runqian.report4.input.RowReportSaver;
import com.runqian.report4.model.ReportDefine;
import com.runqian.report4.model.engine.ExtCellSet;
import com.runqian.report4.model.engine2.RowReport;
import com.runqian.report4.usermodel.Area;
import com.runqian.report4.usermodel.Context;
import com.runqian.report4.usermodel.DataSourceConfig;
import com.runqian.report4.usermodel.Engine;
import com.runqian.report4.usermodel.INormalCell;
import com.runqian.report4.usermodel.IReport;
import com.runqian.report4.usermodel.input.InputProperty;
import com.runqian.report4.util.ReportUtils;

public class DateFromExcel2 {

Context cxt = null;

public void saveFromExcel(String reportFile, String excelFile, int sheetNum)
throws Exception {
IReport report = calcReport(reportFile); //计算出填报表
RowReport rr = ( RowReport ) report;
String did = rr.getDetailID( 2 ); //第二行是扩展主行
//System.out.println(”did–>”+did);
SimpleDateFormat dateF = new SimpleDateFormat( FormatUtils.getDateFormat( Locale.getDefault() ) );
SimpleDateFormat timeF = new SimpleDateFormat( FormatUtils.getTimeFormat( Locale.getDefault() ) );
SimpleDateFormat datetimeF = new SimpleDateFormat( FormatUtils.getDatetimeFormat( Locale.getDefault() ) );
//System.out.println(”datef->”+dateF);
//System.out.println(”timeF->”+timeF);
//System.out.println(”datetimeF->”+datetimeF);
RowReportSaver rowSaver = new RowReportSaver( rr, null, cxt );
IReport excelReport = excelToReport(excelFile,sheetNum);
//int rows = excelReport.getRowCount();
//short cols = excelReport.getColCount();
//if(rows>report.getRowCount()) rows = report.getRowCount();
//if(cols>report.getColCount()) cols = report.getColCount();
for(int i=2;i<=excelReport.getRowCount();i++){ ////逐行逐列把excel数据写入填报表
Area area = rr.insertDetail( did );
for(int j=1;j<=excelReport.getColCount();j++){
INormalCell iExcelCell=excelReport.getCell(i, (short)j); //取得excel单元格
//System.out.println(”iExcelCell.getValue()–>”+iExcelCell.getValue());

//System.out.println(”area.getBeginRow()–>”+area.getBeginRow());
//System.out.println(”i->”+i);
//System.out.println(”(short)j->”+(short)j);
if( iExcelCell == null ) continue;
String svalue = “”;
Object value = iExcelCell.getValue();
if( value != null ) svalue = value.toString();
System.out.println(”svale->”+svalue);
rowSaver.setCellInputValue( area.getBeginRow(), (short)j, svalue, dateF, timeF, datetimeF );
}
}
//ReportUtils.write(”c:/qq.raq”,report);
rowSaver.save();
}

private IReport calcReport(String reportFile) throws Exception {
ReportDefine rd = (ReportDefine) ReportUtils.read(reportFile);
cxt = new Context();

Connection connection = null;
try {
Driver driver = (Driver) Class.forName(”org.hsqldb.jdbcDriver”)
.newInstance();
DriverManager.registerDriver(driver);
connection = DriverManager.getConnection(
“jdbc:hsqldb:hsql://localhost/runqianDB”, “sa”, “”);
} catch (Exception e) {
e.printStackTrace();
}

// 设置数据源,动态改变连接参数
cxt.setDefDataSourceName(”demo”);
// 设置数据连接,也可以设置连接池工厂,如cxt.setConnectionFactory(sourceName,connectionFactory)
// 其中connectionFactory这个类必须implements IConnectionFactory,并且实现他的public
// java.sql.Connection getConnection() throws Exception 方法
cxt.setConnection(”demo”, connection);
DataSourceConfig dsoc = new DataSourceConfig(2, true, “GBK”, “GBK”,
false);
cxt.setDataSourceConfig(”demo”, dsoc);

Engine engine = new Engine(rd, cxt);
IReport iReport = engine.calc();
return iReport;

} private IReport excelToReport(String excelFile, int sheetNum)
throws Exception {
ExcelImporter ei = new ExcelImporter(excelFile);
return ei.getReport(sheetNum);
}

public static void main(String[] args){
String license = “D:\\software\\tomcat\\tomcat5.0.28\\webapps\\report4\\WEB-INF\\2009-03-31V4.0Windows.lic”;
String reportFile = “D:\\software\\tomcat\\tomcat5.0.28\\webapps\\report4\\reportFiles\\newapi\\input.raq”;
String excelFile = “D:\\software\\tomcat\\tomcat5.0.28\\webapps\\report4\\reportFiles\\newapi\\input.xls”;
int sheetNum = 0;
DateFromExcel2 dexcel = new DateFromExcel2();
ExtCellSet.setLicenseFileName( license );
try {
dexcel.saveFromExcel(reportFile,excelFile,sheetNum);
} catch (Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
}

}

然后在前台页面上设置一个按钮来调用这个后台类,就可以实现行式填报表导入excel直接保存数据入库的功能了.