本文主要是介绍strus2将jsp页面的表格导出为excle表格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
只是为了自己以后忘记的时候方便查阅,也有借鉴别人的地方。多学多做多思考。
首先是struts的action配置
//fileName,inputStream都很重要
<action name="dcexcle" class="SpscAction" method="exportExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">inputStream</param>
</result>
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">inputStream</param>
</result>
</action>
其次是action内容了
public String exportExcel() throws Exception{
String name ="商品列表.xls";
try {
name = java.net.URLEncoder.encode(name, "UTF-8");
fileName = new String(name.getBytes(), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
log4j.error("字符转码失败");
}
return SUCCESS;
}
public InputStream getInputStream() throws Exception //getInputStream()一定要与InputStream对应,否则会出现异常
{
List<Object> productList = new ArrayList<Object>();
try {
productList = spscService.selectsp(getParameter());
System.out.println(productList.toString());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//第二步:构建excel表格,封装数据到excel
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);//创建第一行
HSSFCell cell = row.createCell(0);//第一列
cell.setCellValue("商品列表"); //行内容
HSSFCellStyle style = workbook.createCellStyle(); //创建样式
HSSFFont font = workbook.createFont(); //创建字体样式
font.setFontHeight((short)(20*20)); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); //加粗
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中
style.setFont(font); //设置字体样式
cell.setCellStyle(style); //设置样式
sheet.addMergedRegion(new org.apache.poi.ss.util.Region(0,(short)0,0,(short)4)); //合并列
row = sheet.createRow(1);//创建第一行
cell = row.createCell(0); //创建第一列
cell.setCellValue(new HSSFRichTextString("商品编号"));
cell = row.createCell(1);//创建第二列
cell.setCellValue(new HSSFRichTextString("商品名称"));
cell = row.createCell(2);//
cell.setCellValue(new HSSFRichTextString("商品价格"));
cell = row.createCell(3);//
cell.setCellValue(new HSSFRichTextString("商品类型"));
String SPID2;
String SPNAME2;
String SPPRICE2;
String SPLX2;
for (int i = 0; i < productList.size(); i++) {
HashMap<String, Map> hm=new HashMap<String, Map>();
hm.put("SP", (Map) productList.get(i));
Map map=new HashMap();
map.put("SPID", hm.get("SP").get("SPID"));
map.put("SPNAME", hm.get("SP").get("SPNAME"));
map.put("SPPRICE", hm.get("SP").get("SPPRICE"));
map.put("SPLX", hm.get("SP").get("SPLX"));
SPID2=map.get("SPID").toString();
SPNAME2=map.get("SPNAME").toString();
SPPRICE2=map.get("SPPRICE").toString();
SPLX2=map.get("SPLX").toString();
row=sheet.createRow(i+2);//创建第i+1行
cell=row.createCell(0);//创建第一列
cell.setCellValue(SPID2);
cell=row.createCell(1);//创建第二列
cell.setCellValue(SPNAME2);
cell=row.createCell(2);
cell.setCellValue(SPPRICE2);
cell=row.createCell(3);
cell.setCellValue(SPLX2);
}
//第三步:写入输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
workbook.write(baos);//写入
} catch (IOException e) {
e.printStackTrace();
}
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
}
String name ="商品列表.xls";
try {
name = java.net.URLEncoder.encode(name, "UTF-8");
fileName = new String(name.getBytes(), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
log4j.error("字符转码失败");
}
return SUCCESS;
}
public InputStream getInputStream() throws Exception //getInputStream()一定要与InputStream对应,否则会出现异常
{
List<Object> productList = new ArrayList<Object>();
try {
productList = spscService.selectsp(getParameter());
System.out.println(productList.toString());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//第二步:构建excel表格,封装数据到excel
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);//创建第一行
HSSFCell cell = row.createCell(0);//第一列
cell.setCellValue("商品列表"); //行内容
HSSFCellStyle style = workbook.createCellStyle(); //创建样式
HSSFFont font = workbook.createFont(); //创建字体样式
font.setFontHeight((short)(20*20)); //字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); //加粗
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //居中
style.setFont(font); //设置字体样式
cell.setCellStyle(style); //设置样式
sheet.addMergedRegion(new org.apache.poi.ss.util.Region(0,(short)0,0,(short)4)); //合并列
row = sheet.createRow(1);//创建第一行
cell = row.createCell(0); //创建第一列
cell.setCellValue(new HSSFRichTextString("商品编号"));
cell = row.createCell(1);//创建第二列
cell.setCellValue(new HSSFRichTextString("商品名称"));
cell = row.createCell(2);//
cell.setCellValue(new HSSFRichTextString("商品价格"));
cell = row.createCell(3);//
cell.setCellValue(new HSSFRichTextString("商品类型"));
String SPID2;
String SPNAME2;
String SPPRICE2;
String SPLX2;
for (int i = 0; i < productList.size(); i++) {
HashMap<String, Map> hm=new HashMap<String, Map>();
hm.put("SP", (Map) productList.get(i));
Map map=new HashMap();
map.put("SPID", hm.get("SP").get("SPID"));
map.put("SPNAME", hm.get("SP").get("SPNAME"));
map.put("SPPRICE", hm.get("SP").get("SPPRICE"));
map.put("SPLX", hm.get("SP").get("SPLX"));
SPID2=map.get("SPID").toString();
SPNAME2=map.get("SPNAME").toString();
SPPRICE2=map.get("SPPRICE").toString();
SPLX2=map.get("SPLX").toString();
row=sheet.createRow(i+2);//创建第i+1行
cell=row.createCell(0);//创建第一列
cell.setCellValue(SPID2);
cell=row.createCell(1);//创建第二列
cell.setCellValue(SPNAME2);
cell=row.createCell(2);
cell.setCellValue(SPPRICE2);
cell=row.createCell(3);
cell.setCellValue(SPLX2);
}
//第三步:写入输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
workbook.write(baos);//写入
} catch (IOException e) {
e.printStackTrace();
}
byte[] ba = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
return bais;
}
jsp页面就不贴了,也没有什么内容。excle表格填充的内容都是通过list.get(i)填充的。详细了解看另一篇自我笔记http://blog.csdn.net/ruoyang666/article/details/79080274
这篇关于strus2将jsp页面的表格导出为excle表格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!