[版权申明:本文系作者原创,转载请注明出处]

文章出处: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

使用POI实现报表打印功能的更多相关文章

  1. VS2013自带报表+打印功能

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010773667/article/details/27540389 经过了VB版机房收费系统的学习 ...

  2. 使用FastReport.net 报表在网页上实现打印功能

    这些年的工作当中,最早是在8年前接触到FastReport这个报表工具,从名字上来看,直译过来就是快速报表,正所谓天下武功,唯快不破,FastReport报表早些年确实是制作报表的不二之选,8年前的工 ...

  3. 完美演绎DevExpress XtraPrinting Library 的打印功能

    完美演绎DevExpress XtraPrinting Library 的打印功能 2010-05-14 17:40:49|  分类: 默认分类|字号 订阅     设计报告不仅费时间,而且还乏味!但 ...

  4. JS调用水晶报表打印翻页按钮事件

    默认的水晶报表打印按钮.翻页按钮太小,并且样式不好调整,考虑自己做一个按钮,然后调用水晶报表的按钮事件. 在实际操作中发现可以在.net按钮的服务器端事件中调用翻页方法: CrystalReportV ...

  5. vc 实现打印功能

    Visual C++6.0是开发Windows应用程序的强大工具,但是要通过它实现程序的打印功能,一直是初学者的一个难点,经常有朋友询问如何在VC中实现打印功能,他们往往感到在MFC提供的框架内实现这 ...

  6. 微软RDLC报表打印

    关于微软RDLC报表打印时文字拉伸问题(Windows server 2003 sp2) 最近我们开发的打印服务频频出现打印文字拉伸问题,客户意见络绎不绝,最为明显的是使用黑体加粗后 “2.0份” 打 ...

  7. RDIFramework.NET V3.3 Web版新增报表管理功能模块-重量级实用功能

    功能描述 在RDIFramework.NET V3.3 Web版本新增了全新的报表管理功能模块,非常实用的功能,重量级推荐.主要用于对日常常用的报表做定制展示.可以自动发布到模块(就可授权给指定资源访 ...

  8. .NET中使用FastReport实现打印功能

    FastReport是功能非常强大的报表工具,在本篇文章中讲解如何使用FastReport实现打印功能. 一.新建一个窗体程序,窗体上面有设计界面和预览界面两个按钮,分别对应FastReport的设计 ...

  9. Atitit.java swing打印功能 api  attilax总结

    Atitit.java swing打印功能 api  attilax总结 1. 打印方式有三种:2 1.1. 一是不经过任何修改,直接调用javascript中的window.print()打印.2 ...

随机推荐

  1. React-Native(一):React Native环境搭建

    第一步:安装jdk 从java官网下载jdk8 配置环境变量: JAVA_HOME:D:\Program Files\Java\jdk1.8.0_111 Path中追加:%JAVA_HOME%\bin ...

  2. 使用 RHEL(RedHat)6.1 iso 安装包 安装Samba过程

    今天因为工作的需要安装了(RHEL)redhat 6.1 自己为了方便就安装Samba 以记之. 注:Linux系统是刚刚安装好的所以没有samba安装的任何记录. 安装准备: ISO:RHEL_6. ...

  3. POJ-1861 Network---最小生成树

    题目链接: https://vjudge.net/problem/POJ-1861 题目大意: 有一些公司,公司之间需要连接起来.给出了哪些公司可以连接以及连接边的长度.求最小生成树中最大的边,以及最 ...

  4. 微信小程序开发-IP地址查询-例子

    微信小程序开发  小程序搜索框  IP地址查询  搜索查询  样例 微信小程序 开发 参考   https://mp.weixin.qq.com/debug/wxadoc/dev/component/ ...

  5. html标记语言 --表单

    html标记语言 --表单 七.表单 1.表单标记 1.1表单中的内容 <form></form>定义表单的开始位置和结束位置,表单提交时的内容就是<form>表单 ...

  6. Linux下内存问题检测神器:Valgrind

    在写大型C/C++工程时难免会发生内存泄漏现象,系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题.有时这些问题非常琐碎,而更多时候它会演变成一个调 ...

  7. python 3全栈开发-面向对象之绑定方法(classmethod与staticmethod的区别)、多态、封装的特性property

    一.面向对象绑定方法 一.类中定义的函数分成两大类 1.绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): 1. 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 ...

  8. 创建类似于Oracle中decode的函数

    -- 创建类似于Oracle中decode的函数create or replace function decode(variadic p_decode_list text[])returns text ...

  9. 自己使用Vue全家桶问题合集(很多eslint规范问题)

    遇到很多问题一一道来. 1.vue报错 Do not use built-in or reserved HTML elements as component id:header 组件,不能和html标 ...

  10. 一个web程序员的年终总结

    2017年年终总结(就是一个程序员的瞎叨叨): 从来到中科院到现在,很开心可以在这留下来.毕竟对于我来说,这里符合我对自己毕业后前两年的规划.我是一个很慢的人,特别是对于我想做好的事情,我会非常认真仔 ...