用POI加载模板生成excel报表
2008-03-15 01:15
429 查看
加载模板生成excel报表
----------------------------------------------------------------------------------------------------------------------------------------
package com.stake.mis.baobiao;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import com.stake.mis.bean.User ;
public class ExeclManager {
/**
* list 存放查询返回得结果列表
* sheetname excel模板中sheet得名字
* out 输出流对象
/
public void getMainCardExcel(List list,String filepath,String sheetname,ServletOutputStream out) throws FileNotFoundException, IOException
{
// 读取工作簿
System.out.println(filepath);
HSSFWorkbook workbook =new HSSFWorkbook(new FileInputStream(filepath));
try
{
//读取工作表
HSSFSheet sheet = workbook.getSheet(sheetname);
HSSFRow row;
HSSFCell cell = null;
int a = 8;
HSSFCellStyle style = this.getStyle(workbook);
for(int i =0;i<list.size();i++)
{
//从查询结果列表中获取对象
User user = (User )list.get(i);
//该行以前得部分从模板中取得;
row = sheet.createRow(a);
cell = row.createCell((short) 0,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i+1);
cell.setCellStyle(style);
cell = row.createCell((short) 1,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mc.getUserName());
cell.setCellStyle(style);
cell = row.createCell((short) 2,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mc.getUserAge());
cell.setCellStyle(style);
cell = row.createCell((short) 3,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mc.get某属性());
cell.setCellStyle(style);
cell = row.createCell((short) 4,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mc.getEq某属性());
cell.setCellStyle(style);
a++;
}
workbook.write(out);
out.flush();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public HSSFCellStyle getStyle(HSSFWorkbook workbook)
{
// 设置字体;
HSSFFont font = workbook.createFont();
//设置字体大小;
font.setFontHeightInPoints((short)9);
//设置字体名字;
font.setFontName("Courier New");
//font.setItalic(true);
//font.setStrikeout(true);
// 设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
}
strut的action某方法中在获取查询结果后的部分代码
-------------------------------------------------------------------------------------------------------------------------------------------
List list = null;
String filename = "";
//action中调用service对象的方法,查询User信息;
list = service.queryUser(查询约束);
ExeclManager ma = new ExeclManager();
//设置响应的编码方式;
response.setCharacterEncoding("gb2312");
filename = filename + "设备台账.xls";
response.addHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(filename, "utf-8"));
response.setContentType("application/vnd.ms-excel;charset=gb2312");
//excel模板所在的位置;
String filepath = this.getServlet().getServletContext().getRealPath("/bbmanage");
ServletOutputStream out = response.getOutputStream();
//模板名称为example.xls;Sheet1为模板中sheet的名称
//调用方法生成excel;
ma.getMainCardExcel(list, filepath+"/example.xls", "Sheet1",out);
----------------------------------------------------------------------------------------------------------------------------------------
package com.stake.mis.baobiao;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import com.stake.mis.bean.User ;
public class ExeclManager {
/**
* list 存放查询返回得结果列表
* sheetname excel模板中sheet得名字
* out 输出流对象
/
public void getMainCardExcel(List list,String filepath,String sheetname,ServletOutputStream out) throws FileNotFoundException, IOException
{
// 读取工作簿
System.out.println(filepath);
HSSFWorkbook workbook =new HSSFWorkbook(new FileInputStream(filepath));
try
{
//读取工作表
HSSFSheet sheet = workbook.getSheet(sheetname);
HSSFRow row;
HSSFCell cell = null;
int a = 8;
HSSFCellStyle style = this.getStyle(workbook);
for(int i =0;i<list.size();i++)
{
//从查询结果列表中获取对象
User user = (User )list.get(i);
//该行以前得部分从模板中取得;
row = sheet.createRow(a);
cell = row.createCell((short) 0,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i+1);
cell.setCellStyle(style);
cell = row.createCell((short) 1,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mc.getUserName());
cell.setCellStyle(style);
cell = row.createCell((short) 2,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mc.getUserAge());
cell.setCellStyle(style);
cell = row.createCell((short) 3,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mc.get某属性());
cell.setCellStyle(style);
cell = row.createCell((short) 4,HSSFCellStyle.ALIGN_CENTER);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(mc.getEq某属性());
cell.setCellStyle(style);
a++;
}
workbook.write(out);
out.flush();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public HSSFCellStyle getStyle(HSSFWorkbook workbook)
{
// 设置字体;
HSSFFont font = workbook.createFont();
//设置字体大小;
font.setFontHeightInPoints((short)9);
//设置字体名字;
font.setFontName("Courier New");
//font.setItalic(true);
//font.setStrikeout(true);
// 设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
}
strut的action某方法中在获取查询结果后的部分代码
-------------------------------------------------------------------------------------------------------------------------------------------
List list = null;
String filename = "";
//action中调用service对象的方法,查询User信息;
list = service.queryUser(查询约束);
ExeclManager ma = new ExeclManager();
//设置响应的编码方式;
response.setCharacterEncoding("gb2312");
filename = filename + "设备台账.xls";
response.addHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(filename, "utf-8"));
response.setContentType("application/vnd.ms-excel;charset=gb2312");
//excel模板所在的位置;
String filepath = this.getServlet().getServletContext().getRealPath("/bbmanage");
ServletOutputStream out = response.getOutputStream();
//模板名称为example.xls;Sheet1为模板中sheet的名称
//调用方法生成excel;
ma.getMainCardExcel(list, filepath+"/example.xls", "Sheet1",out);
相关文章推荐
- poi 读取Excel文件模板生成报表文件
- javaweb 利用POI生成excel报表 相关代码
- Java poi实现导出excel,添加数据有效性,生成模板
- java poi 生成excel模板
- java生成excel报表(poi)
- poi excel报表生成的流程和例子
- poi生成excel报表合并列
- POI3.10读取Excel模板填充数据后生成新的Excel文件
- java poi 通过excel模板导出并且生成多个sheet
- 使用POI生成Excel报表
- Excel 格式报表生成 POI
- java poi 通过excel模板导出并且生成多个sheet
- POI生成excel报表
- poi实现根据excel模板,生成excel并导入数据
- 根据模板导出Excel报表并复制模板生成多个Sheet页
- 借助POI实现Java生成并打印报表(Excel)
- jfreechart,pdf生成组件iText,jasper report报表组件及POI操作excel等在企业软件开发中常遇到的第三方应用
- poi jxl 生成EXCEL 报表
- 10、借助POI实现Java生成并打印excel报表(1)
- POI+JFreeChart生成报表图片在Excel中的位置