java 利用org.apache.poi.hssf包编写excel导出文件
2015-08-10 18:22
711 查看
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PoiExlUtil {
/*通用工具方法*/
public static HSSFWorkbook getExcel(String sql,String sheetName){
Connection conn=DBUtil.getConnection();
Statement st=null;
ResultSet rs=null;
try {
st=conn.createStatement();
rs=st.executeQuery(sql);
HSSFWorkbook workbook=new HSSFWorkbook(); //新建一个excel文件
HSSFSheet sheet=workbook.createSheet(sheetName); //建立序表名
int j=0;
//生成标题
HSSFRow row=null;
HSSFCell cell=null;
ResultSetMetaData rsmd=rs.getMetaData();
row=sheet.createRow(j);
int size=rsmd.getColumnCount();
for (int i = 0; i <size; i++) {
cell=row.createCell((short)i);//1行中有几列
cell.setCellValue(new HSSFRichTextString(rsmd.getColumnLabel(i+1)));//循环每列赋值
}
//生成数据域
while(rs.next()){
j++;
row=sheet.createRow(j);
for (int i = 0; i <size; i++) {
cell=row.createCell((short)i);
cell.setCellValue(new HSSFRichTextString(rs.getString(i+1)));
}
sheet.autoSizeColumn((short)j);
}
return workbook;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
DBUtil.close(rs, st, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
step2.
HSSFWorkbook workbook=PoiExlUtil.getExcel(sql, "sheet名");
try {
4000
//设置编码格式
String showName = URLEncoder.encode("文件名"+DateUtil.getCurrentDateStr("yyyyMMddhhmmmss")+".xls", "UTF-8");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename="
+ showName);
workbook.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PoiExlUtil {
/*通用工具方法*/
public static HSSFWorkbook getExcel(String sql,String sheetName){
Connection conn=DBUtil.getConnection();
Statement st=null;
ResultSet rs=null;
try {
st=conn.createStatement();
rs=st.executeQuery(sql);
HSSFWorkbook workbook=new HSSFWorkbook(); //新建一个excel文件
HSSFSheet sheet=workbook.createSheet(sheetName); //建立序表名
int j=0;
//生成标题
HSSFRow row=null;
HSSFCell cell=null;
ResultSetMetaData rsmd=rs.getMetaData();
row=sheet.createRow(j);
int size=rsmd.getColumnCount();
for (int i = 0; i <size; i++) {
cell=row.createCell((short)i);//1行中有几列
cell.setCellValue(new HSSFRichTextString(rsmd.getColumnLabel(i+1)));//循环每列赋值
}
//生成数据域
while(rs.next()){
j++;
row=sheet.createRow(j);
for (int i = 0; i <size; i++) {
cell=row.createCell((short)i);
cell.setCellValue(new HSSFRichTextString(rs.getString(i+1)));
}
sheet.autoSizeColumn((short)j);
}
return workbook;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
DBUtil.close(rs, st, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
step2.
HSSFWorkbook workbook=PoiExlUtil.getExcel(sql, "sheet名");
try {
4000
//设置编码格式
String showName = URLEncoder.encode("文件名"+DateUtil.getCurrentDateStr("yyyyMMddhhmmmss")+".xls", "UTF-8");
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename="
+ showName);
workbook.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- Apache Shiro 标签方式授权
- CentOS安装Apache详解
- Apache001_ 模块介绍
- 访问jsp页面报错:org.apache.jasper.JasperException: java.lang.NullPointerException
- windows开启Apache的mod_rewrite模块
- apache配置详解
- jmeter与apache测试网站并发
- apache mvc的运用
- Apache服务器 403
- Spring中的BeanUtils与apache commons中的BeanUtils用法[1]
- apache服务器安装以及使用passenger插件部署rails应用
- apache 访问权限基本设置
- Centos 6.3 安装apache
- ubuntu 安装apache日志分析软件awstats
- 四大开源协议比较:BSD、Apache、GPL、LGPL
- XAMPP启动Apache:Port 443 in use by VMware-hosted.exe
- MAC OSX10.10上搭建Apache,PHP,MySQL5.6.22,phpMyAdmin开发环境
- Apache 2.2.10+Apusic 5.1负载均衡应用实践
- apache StringUtils
- apache