您的位置:首页 > 其它

使用POI实现报表打印功能

2016-11-29 15:34 357 查看
[版权申明:本文系作者原创,转载请注明出处]

文章出处:http://blog.csdn.net/sdksdk0/article/details/53393453

作者:朱培 ID:sdksdk0

这篇文章主要分享的是使用apache的poi来实现数据导出到excel的功能,这里提供三种解决方案。你可以使用最原始最简单的一步步添加样式或者数据,你也可以通过一个模板来进行模板化的导出,也可以对百万级数据进行到处。现在很多人提供导出功能是不支持大数据量的导出的,我记得有的朋友导出3-4万条数据系统就挂掉了。所以对于大量数据导出,本文也提供解决方案。

1、POI解决的问题

关于POI的简介,我这里就不再提及,你可以参考这篇文章:http://blog.csdn.net/sdksdk0/article/details/52557755。

JXL,POI都是操作excel

Excel一些企业小的应用都直接用excel来实现,例如:工资报表,人员名单,进销存

作为数据的备份和恢复(导入、导出)

Jxl它只能操作excel 2003版本,它导入导出数据量小时性能很高

POI 它可以操作office系列软件word、excel、ppt、visio(画网络布局、家装),在早期版本中它在导出海量数据时,容易崩溃。在新版本中它解决了这个海量数据时,进行了优化,解决了这个问题。我们现在一般是使用3.0版本之后的。

目前常见读写Excel的工具类开源javaAPI有两种方式,

一个是JXL(Java Excel API) 官网地址:http://jexcelapi.sourceforge.net/

一个是Apache的POI(Poor Obfuscation Implementation)官网地址:http://poi.apache.org/

POI支持微软的OLE2格式文件Office 2003及以下版本;同时支持微软的OOXML(Office Open XML)标准,也就是Office 2007以上版本。JXL只能实现对Excel 2003以下版本的支持。

POI使用HFFS对象操作OLE2格式Excel,文件后缀为.xls的;使用XSSF、SXSSF对象操作OOXML格式Excel,文件后缀为.xlsx的。

对于OLE2版本的Excel,一个Sheet工作表它的行最多支持到65536行,列支持到256列;

对于OOXML版本的Excel,一个Sheet工作表它的行支持到1048576行,列支持到65536列。

2.POI的入门

当然咯,如果你的是maven工程,需要先把坐标导进来。不然怎么运行呢,你说是吧,哈哈!

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>


如果你的是普通的工程,那么自己导个jar包进去吧!

使用poi只需要7个步骤,非常简单。

//基本使用

@Test
public void HSSF() throws IOException{
//1.创建一个工作簿
Workbook wb=new HSSFWorkbook();
//2.创建一个工作表sheet
Sheet  sheet=wb.createSheet();
//3.创建一个行对象
Row  nRow=sheet.createRow(4); //从0开始
//4、创建一个单元格对象,指定列
Cell nCell=nRow.createCell(4);
//5、设置内容
nCell.setCellValue("指令汇科技");
//6.保存
OutputStream stream=new FileOutputStream(new File("D:\\test1.xls"));
wb.write(stream);
//7.关闭
stream.close();

}


前面的这种方式导出来的excel表格都是非常原始的,非常简陋的,那么我们想添加一些样式怎么办呢?

so,我们可以使用下面这种方式:我们可以使用CellStyle来设置样式。

@Test
public void HSSF1() throws IOException{
//1.创建一个工作簿
Workbook wb=new HSSFWorkbook();
//2.创建一个工作表sheet
Sheet  sheet=wb.createSheet();
//3.创建一个行对象
Row  nRow=sheet.createRow(4); //从0开始
//4、创建一个单元格对象,指定列
Cell nCell=nRow.createCell(4);
//5、设置内容
nCell.setCellValue("指令汇科技");

CellStyle titleStyle=wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("微软雅黑");  //设置字体类型
font.setFontHeightInPoints((short) 26);  //设置字体大小
titleStyle.setFont(font);

nCell.setCellStyle(titleStyle);
//6.保存
OutputStream stream=new FileOutputStream(new File("D:\\test1.xls"));
wb.write(stream);
//7.关闭
stream.close();

}


3.报表打印

说到这里,我就需要打印一个如下图所示的表。



这些数据我都是从数据库里面查询出来的,和我的业务是有关系的咯,这个如何从数据库里面把这些数据查出来我就不说了,这里假设你已经有这些数据了,(真正感兴趣的,可以去下载我的这个项目源码查看哦,文末提供下载地址)。

关于把这个报表打印出来,这里我提供3种方式

方案1

就是直接在代码里面设置表格的列宽、样式等。

1、创建一个工作簿:

Workbook wb=new HSSFWorkbook();


2、…反正就是前面说的那7个步骤啦!。

这里我要说的是添加样式。

对于大标题:例如我这里是2016年11月出货表。

//大标题的样式
public CellStyle bigTitle(Workbook wb,CellStyle nStyle,Font font){
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//横向居中
nStyle.setAlignment(CellStyle.ALIGN_CENTER);
//纵向居中
nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);     //单元格垂直居中

nStyle.setFont(font);
return nStyle;
}


然后代在码中进行调用:

//大标题,合并单元格
sheet.addMergedRegion(new CellRangeAddress(0,0,1,9));  //开始行,结束行,开始列,结束列
//合并单元格的内容写在合并前第一个单元格中
nRow=sheet.createRow(rowNo++);

nRow.setHeightInPoints(36);

nCell=nRow.createCell(1);
nCell.setCellValue(inputDate.replace("-0", "年").replaceFirst("-", "年")+"月份出货表");
nCell.setCellStyle(this.bigTitle(wb, nStyle, font));


然后是标题栏,就是我的那个客户、订单号这些内容哈!

//标题样式
public CellStyle Title(Workbook wb,CellStyle nStyle,Font font){
font.setFontName("黑体");
font.setFontHeightInPoints((short) 12);

//横向居中
nStyle.setAlignment(CellStyle.ALIGN_CENTER);
//纵向居中
nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);     //单元格垂直居中

//表格线
nStyle.setBorderTop(CellStyle.BORDER_THICK);            //粗实线
nStyle.setBorderBottom(CellStyle.BORDER_THIN);          //实线
nStyle.setBorderLeft(CellStyle.BORDER_THIN);
nStyle.setBorderRight(CellStyle.BORDER_THIN);

nStyle.setFont(font);
return nStyle;
}


在代码中调用:

String[] title=new     String[]{"客户","订单号","货号","数量","工厂","附件","工厂交期","船期","贸易条款"  };

nRow=sheet.createRow(rowNo++);
nRow.setHeightInPoints(26.25f);

//初始化
nStyle=wb.createCellStyle();
font=wb.createFont();

for(int  i=0;i<title.length;i++){
nCell=nRow.createCell(i+1);
nCell.setCellValue(title[i]);
nCell.setCellStyle(this.Title(wb, nStyle, font));

}


接下来是内容,这里的填充数据都是从数据库中查询出来的:

//文字样式
public CellStyle Text(Workbook wb,CellStyle nStyle,Font font){
font.setFontName("Times New Roman");
font.setFontHeightInPoints((short) 10);

//横向居中
nStyle.setAlignment(CellStyle.ALIGN_CENTER);
//纵向居中
nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);     //单元格垂直居中

//表格线

nStyle.setBorderBottom(CellStyle.BORDER_THIN);          //实线
nStyle.setBorderLeft(CellStyle.BORDER_THIN);
nStyle.setBorderRight(CellStyle.BORDER_THIN);           //实线

nStyle.setFont(font);
return nStyle;
}


填充数据:

//初始化
nStyle=wb.createCellStyle();
font=wb.createFont();

//换行
for(int j=0;j<dataList.size();j++){
OutProductVO op=dataList.get(j);
colNo=1;

nRow=sheet.createRow(rowNo++);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getCustomName());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getProductNo());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getContractNo());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getCnumber());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getFactoryName());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getExts());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getDeliveryPeriod());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getSpipTime());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getTradeTerms());
nCell.setCellStyle(this.Text(wb, nStyle, font));

}


最后提供一个下载的方法:

//下载
DownloadUtil dUtil=new DownloadUtil();
ByteArrayOutputStream os=new ByteArrayOutputStream();
wb.write(os);
dUtil.download(os, response, "出货表.xls");


完整代码如下:

//方案1
@RequestMapping("/cargo/outproduct/print.action")
public void print(String inputDate,HttpServletResponse  response) throws IOException{
List<OutProductVO> dataList = outProductService.find(inputDate);

Workbook wb=new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row nRow=null;
Cell nCell=null;

//行号
int  rowNo=0;
//列号
int colNo=1;

//声明样式对象和字体对象
CellStyle nStyle=wb.createCellStyle();
Font font = wb.createFont();

//列宽
sheet.setColumnWidth(0,2*300);
sheet.setColumnWidth(1,26*300);
sheet.setColumnWidth(2,12*300);
sheet.setColumnWidth(3,29*300);
sheet.setColumnWidth(4,10*300);
sheet.setColumnWidth(5,12*300);
sheet.setColumnWidth(6,8*300);
sheet.setColumnWidth(7,10*300);
sheet.setColumnWidth(8,10*300);
sheet.setColumnWidth(9,9*300);

//大标题,合并单元格
sheet.addMergedRegion(new CellRangeAddress(0,0,1,9));  //开始行,结束行,开始列,结束列
//合并单元格的内容写在合并前第一个单元格中
nRow=sheet.createRow(rowNo++);

nRow.setHeightInPoints(36);

nCell=nRow.createCell(1);
nCell.setCellValue(inputDate.replace("-0", "年").replaceFirst("-", "年")+"月份出货表");
nCell.setCellStyle(this.bigTitle(wb, nStyle, font));

String[] title=new  String[]{"客户","订单号","货号","数量","工厂","附件","工厂交期","船期","贸易条款"  };

nRow=sheet.createRow(rowNo++);
nRow.setHeightInPoints(26.25f);

//初始化
nStyle=wb.createCellStyle();
font=wb.createFont();

for(int  i=0;i<title.length;i++){
nCell=nRow.createCell(i+1);
nCell.setCellValue(title[i]);
nCell.setCellStyle(this.Title(wb, nStyle, font));

}
//初始化
nStyle=wb.createCellStyle();
font=wb.createFont();

//换行
for(int j=0;j<dataList.size();j++){
OutProductVO op=dataList.get(j);
colNo=1;

nRow=sheet.createRow(rowNo++);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getCustomName());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getProductNo());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getContractNo());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getCnumber());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getFactoryName());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getExts());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getDeliveryPeriod());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getSpipTime());
nCell.setCellStyle(this.Text(wb, nStyle, font));

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getTradeTerms());
nCell.setCellStyle(this.Text(wb, nStyle, font));

}

//OutputStream  os=new FileOutputStream(new File("D:\\outProduct.xls"));
/*wb.write(os);
os.flush();
os.close();*/

//下载
DownloadUtil dUtil=new DownloadUtil();
ByteArrayOutputStream os=new ByteArrayOutputStream();
wb.write(os);
dUtil.download(os, response, "出货表.xls");

}

//大标题的样式
public CellStyle bigTitle(Workbook wb,CellStyle nStyle,Font font){
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//横向居中
nStyle.setAlignment(CellStyle.ALIGN_CENTER);
//纵向居中
nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);     //单元格垂直居中

nStyle.setFont(font);
return nStyle;
}

//标题样式
public CellStyle Title(Workbook wb,CellStyle nStyle,Font font){
font.setFontName("黑体");
font.setFontHeightInPoints((short) 12);

//横向居中
nStyle.setAlignment(CellStyle.ALIGN_CENTER);
//纵向居中
nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);     //单元格垂直居中

//表格线
nStyle.setBorderTop(CellStyle.BORDER_THICK);            //粗实线
nStyle.setBorderBottom(CellStyle.BORDER_THIN);          //实线
nStyle.setBorderLeft(CellStyle.BORDER_THIN);            //比较粗实线
nStyle.setBorderRight(CellStyle.BORDER_THIN);           //实线

nStyle.setFont(font);
return nStyle;
}

//文字样式
public CellStyle Text(Workbook wb,CellStyle nStyle,Font font){
font.setFontName("Times New Roman");
font.setFontHeightInPoints((short) 10);

//横向居中
nStyle.setAlignment(CellStyle.ALIGN_CENTER);
//纵向居中
nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);     //单元格垂直居中

//表格线

nStyle.setBorderBottom(CellStyle.BORDER_THIN);          //实线
nStyle.setBorderLeft(CellStyle.BORDER_THIN);            //比较粗实线
nStyle.setBorderRight(CellStyle.BORDER_THIN);           //实线

nStyle.setFont(font);
return nStyle;
}


方案2

前面提到的方法是要一个个的设置样式,感觉还是比较复杂的,那么如果我们提前做好一个模板,然后把通过代码看来读取模板的excel文件中的样式,然后按照这种模板的格式再去填充数据的话,效果应该会更好一点的噢!

1、首先要获取模板文件的路径,就是你把这个文件放在哪里了。

//获取模板存放的路径
String path=request.getSession().getServletContext().getRealPath("/")+"/make/xlsprint/";
InputStream  is=new FileInputStream(new File(path+"出货表.xls"));


2、获取模板上的单元格样式

nRow=sheet.getRow(2);


3、获取需要的样式。getCell()指的就是从第几列获取。我们是从0开始计算的。

//客户的样式
nCell=nRow.getCell(1);
CellStyle customStyle=nCell.getCellStyle();
//订单的样式
nCell=nRow.getCell(2);
CellStyle contractNoStyle=nCell.getCellStyle();
//货号的样式
nCell=nRow.getCell(3);
CellStyle productNoStyle=nCell.getCellStyle();
//数量的样式
nCell=nRow.getCell(4);
CellStyle numStyle=nCell.getCellStyle();
//生产厂家的样式
nCell=nRow.getCell(5);
CellStyle factoryStyle=nCell.getCellStyle();
//日期的样式
nCell=nRow.getCell(6);
CellStyle dateStyle=nCell.getCellStyle();
//贸易条款
nCell=nRow.getCell(8);
CellStyle tradeStyle=nCell.getCellStyle();


4、把样式设置到表格的值中。

nCell.setCellStyle(customStyle);


完整代码如下:

//方案2
//模板
@RequestMapping("/cargo/outproduct/printTemple.action")
public void printTemple(String inputDate,HttpServletRequest request,HttpServletResponse  response) throws IOException{
List<OutProductVO> dataList = outProductService.find(inputDate);

//获取模板存放的路径
String path=request.getSession().getServletContext().getRealPath("/")+"/make/xlsprint/";
InputStream  is=new FileInputStream(new File(path+"出货表.xls"));

Workbook wb=new HSSFWorkbook(is);
Sheet sheet = wb.getSheetAt(0);
Row nRow=null;
Cell nCell=null;

//行号
int  rowNo=0;
//列号
int colNo=1;

//获取模板上的单元格样式

nRow=sheet.getRow(2);

//客户的样式
nCell=nRow.getCell(1);
CellStyle customStyle=nCell.getCellStyle();
//订单的样式
nCell=nRow.getCell(2);
CellStyle contractNoStyle=nCell.getCellStyle();
//货号的样式
nCell=nRow.getCell(3);
CellStyle productNoStyle=nCell.getCellStyle();
//数量的样式
nCell=nRow.getCell(4);
CellStyle numStyle=nCell.getCellStyle();
//生产厂家的样式
nCell=nRow.getCell(5);
CellStyle factoryStyle=nCell.getCellStyle();
//日期的样式
nCell=nRow.getCell(6);
CellStyle dateStyle=nCell.getCellStyle();
//贸易条款
nCell=nRow.getCell(8);
CellStyle tradeStyle=nCell.getCellStyle();

//大标题
nRow=sheet.getRow(rowNo++);  //获取一个行对象
nCell=nRow.getCell(colNo);  //获取一个单元格对象
nCell.setCellValue(inputDate.replace("-0", "年").replaceFirst("-", "年")+"月份出货表");

//跳过静态表格头
rowNo++;

//换行
for(int j=0;j<dataList.size();j++){
OutProductVO op=dataList.get(j);
colNo=1;

nRow=sheet.createRow(rowNo++);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getCustomName());
nCell.setCellStyle(customStyle);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getContractNo());
nCell.setCellStyle(contractNoStyle);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getProductNo());
nCell.setCellStyle(productNoStyle);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getCnumber());
nCell.setCellStyle(numStyle);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getFactoryName());
nCell.setCellStyle(factoryStyle);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getDeliveryPeriod());
nCell.setCellStyle(dateStyle);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getSpipTime());
nCell.setCellStyle(dateStyle);

nCell=nRow.createCell(colNo++);
nCell.setCellValue(op.getTradeTerms());
nCell.setCellStyle(tradeStyle);
}

//下载
DownloadUtil dUtil=new DownloadUtil();
ByteArrayOutputStream os=new ByteArrayOutputStream();
wb.write(os);
dUtil.download(os, response, "出货表.xls");
os.flush();
os.close();

}


方案3

嗯,好吧,你可能觉得上面这个模板方式还挺好的,好吧,那我们再来做一个优化!我在开篇就提到了,有的朋友导出数据3-4万的时候系统就撑不下去了,所以我们对于百万级的数据导出还需要进行优化。其实非常简单。

我们前面导出的数据是一个xls的文件,熟悉word的朋友都知道,office1997-2003版本的excel是xls版本的。我们同样还有2007及其以上的版本,是xlsx格式的后缀文件。所以我们可以把文件导出为xlsx的文件。

修改:

Workbook wb=new HSSFWorkbook(is);
Sheet sheet = wb.getSheetAt(0);




Workbook wb=new XSSFWorkbook(is);  //2007版本
Sheet sheet = wb.getSheetAt(0);


就可以了,是不是非常简单,哈哈,当然咯,你的这个模板文件也需要换成xlsx的文件噢,导出文件也需要换成xlsx格式的。

4、excel数据批量导入

如果有需要的朋友,也可以尝试一下批量导入功能。

//导入
@RequestMapping("/basicinfo/factory/importInfo.action")
public String importInfo() throws InvalidFormatException, IOException{
/*
* 开发步骤:
* 1、读取xls文件;创建模板,设置所有单元格为文本类型,然后黏贴数据到editplus,再黏贴到excel中,这样就不怕读取类型错误。
* 2、拼接成sql
* 3、批量插入
*/

//实现自动识别读取的xls版本,创建其对应的对象; 2003 HSSFWorkbook对象;2007 XSSFWorkbook对象
String sql = "";
File file = new File("c:\\7F28E200.xlsx");
Workbook wb = new WorkbookFactory().create(file);       //自动识别excel版本
Sheet sheet = wb.getSheetAt(0);

Row nRow = sheet.getRow(1);
Cell nCell = null;

//是否标题行,标题行还可以多列
int beginRowNo = 0;                             //开始行
int endRowNo =  sheet.getLastRowNum();          //结束行
int beginColNo = 0;                             //开始列
int endColNo = nRow.getLastCellNum();           //结束列

StringBuffer sBuf = new StringBuffer();
for(int i=beginRowNo;i<endRowNo;i++){
nRow = sheet.getRow(i);                     //行对象
//
//          for(int j=beginColNo;j<endColNo;j++){
//              nCell = nRow.getCell(j++);              //单元格
//              System.out.println(nCell.toString());
//          }

beginColNo = 0;                             //开始列
`// `

sBuf.append("insert into factory_c (FACTORY_ID,FULL_NAME,FACTORY_NAME,CONTACTS,PHONE,MOBILE,FAX,INSPECTOR,CNOTE,ORDER_NO,STATE,CREATE_BY,CREATE_DEPT,CREATE_TIME) ");
sBuf.append("values(");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

//orderNo
nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("null");
}else{
sBuf.append(UtilFuns.convertNull(nCell.getStringCellValue()));
}
sBuf.append(",");

//state
nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("null");
}else{
sBuf.append(UtilFuns.convertNull(nCell.getStringCellValue()));
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("''");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}
sBuf.append(",");

nCell = nRow.getCell(beginColNo++);
if(nCell==null){
sBuf.append("NULL");
}else{
sBuf.append("'").append(UtilFuns.convertNull(nCell.getStringCellValue())).append("'");
}

sBuf.append(");");

}
for(String s : sBuf.toString().split(";")){
System.out.println(s);
}

sqlDao.batchSQL(sBuf.toString().split(";"));        //执行insert sql

return "redirect:/basicinfo/factory/list.action";
}


关于POI的文件下载你也可以参考这篇文章:http://blog.csdn.net/article/detailssdksdk0//52557755

源码下载:https://github.com/sdksdk0/JK

关于poi的一些基本操作方法和api文档下载地址:http://download.csdn.net/detail/sdksdk0/9696976
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: