Java JSP页面导出Excel
来源:互联网 发布:呀安拉乎呀阿拉伯歌曲 编辑:程序博客网 时间:2024/05/29 12:01
JSP中:
<input type="button" value="导出" onclick="exportData()"/>
//单击"导出"按钮时
function exportData(){
var dispNo=$("#dispNo").val();
var dispName=$("#dispName").val();
var inWh=$("#inWh").val();
var outWh=$("#outWh").val();
var beginTime=$("#beginTime").val();
var endTime=$("#endTime").val();
var confirmType=$("#confirmSelectType").val();
var action="${pageContext.request.contextPath}/kgBuDispatchformAction!exportData.ilf? dispNo="+dispNo+"&dispName="+dispName+"&inWh="+inWh+"&outWh="+outWh+"&beginTime="+beginTime+"&endTime="+endTime+"&confirmType="+confirmType;
document.forms[0].action = action;
document.forms[0].submit();
}
Action中:
public void exportData() throws IOException{
KgBuDispatchform f = new KgBuDispatchform();
f.setDispNo(getRequest().getParameter("dispNo"));
f.setDispName(getRequest().getParameter("dispName"));
f.setOutWh(getRequest().getParameter("outWh"));
f.setInWh(getRequest().getParameter("inWh"));
f.setBeginTime(getRequest().getParameter("beginTime"));
f.setEndTime(getRequest().getParameter("endTime"));
String confirmType=getRequest().getParameter("confirmType");
if(confirmType.equals("1")){
f.setIsOut(1);
}else if(confirmType.equals("2")){
f.setIsIn(1);
}
export(f);
}
public void export(KgBuDispatchform dispatch){
super.getResponse().setContentType("octets/stream");
String excelName = "调拨单信息表";
try {
//转码防止乱码
super.getResponse().addHeader("Content-Disposition", "attachment;filename="+new String( excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ExportExcel<BaseKgBuDispatchform> ex2 = new ExportExcel<BaseKgBuDispatchform>();
String[] headers2 = { "调拨单编号", "调拨单名称", "调出仓库", "调入仓库", "出库确认标识","入库确认标识" };
List<BaseKgBuDispatchform> dataset2 = new ArrayList<BaseKgBuDispatchform>();
try {
//List<BaseKgBuDispatchform> formList = kgBuDispatchformService.listByHQL(" select t.dispNo,t.dispName,t.outWh,t.inWh,t.isOut,t.isIn from KgBuDispatchform t");
List<BaseKgBuDispatchform> formList = kgBuDispatchformService.listByObject(dispatch);
Iterator iterator = formList.iterator();
/*while(iterator.hasNext()){//你返回的结果集都封装到了Object数组中。
Object[] obj = (Object[])iterator.next();
BaseKgBuDispatchform f = new KgBuDispatchform();
f.setDispNo(obj[0].toString());
f.setDispName(obj[1].toString());
f.setOutWh(obj[2].toString());
f.setInWh(obj[3].toString());
if(null==obj[4]){
f.setIsOut(0);
}else{
f.setIsOut(Integer.parseInt(obj[4].toString()));
}
if(null==obj[5]){
f.setIsIn(0);
}else{
f.setIsIn(Integer.parseInt(obj[5].toString()));
}
dataset2.add(f);
} */
while(iterator.hasNext()){
BaseKgBuDispatchform f = (BaseKgBuDispatchform) iterator.next();
//调入仓库名称
String inWh = kgDicWarehouseService.getKgDicWarehouse(Long.valueOf(f.getInWh())).getName();
f.setInWh(inWh);
String outWh = kgDicWarehouseService.getKgDicWarehouse(Long.valueOf(f.getOutWh())).getName();
f.setOutWh(outWh);
dataset2.add(f);
}
String[] strs={"dispNo","dispName","outWh","inWh","isOut","isIn"};
OutputStream out2 = super.getResponse().getOutputStream();
ex2.exportExcel(headers2, dataset2, out2,strs);
out2.close();
//JOptionPane.showMessageDialog(null, "导出成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Excel导出工具类:
package com.inspur.kcgl.export.service;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
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;
import org.apache.poi.hssf.util.HSSFColor;
import com.inspur.common.basic.BaseAction;
public class ExportExcel<T> extends BaseAction{
public void exportExcel(Collection<T> dataset, OutputStream out,String[] strs) {
exportExcel("测试POI导出EXCEL文档", null, dataset, out, "yyyy-MM-dd",strs);
}
public void exportExcel(String[] headers, Collection<T> dataset,
OutputStream out,String[] strs) {
exportExcel("测试POI导出EXCEL文档", headers, dataset, out, "yyyy-MM-dd",strs);
}
public void exportExcel(String[] headers, Collection<T> dataset,
OutputStream out, String pattern,String[] strs) {
exportExcel("测试POI导出EXCEL文档", headers, dataset, out, pattern,strs);
}
public void exportExcel(String title, String[] headers,
Collection<T> dataset, OutputStream out, String pattern,String[] strs) {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
// 声明一个画图的顶级管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
0, 0, 0, (short) 4, 2, (short) 6, 5));
// 设置注释内容
comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
comment.setAuthor("leno");
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行
Iterator<T> it = dataset.iterator();
int index = 0;
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
T t = (T) it.next();
// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
// Field[] fields = t.getClass().getDeclaredFields();
Field[] fields = t.getClass().getSuperclass().getDeclaredFields(); //查询父类方法
for (int i = 0; i < fields.length; i++) {
for(int j=0;j<strs.length;j++){
if(fields[i].getName().equals(strs[j])){
HSSFCell cell = row.createCell(j);
cell.setCellStyle(style2);
Field field = fields[i];
String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
//Class tCls = t.getClass();
Class tCls = t.getClass().getSuperclass();//查询父类
Method getMethod = tCls.getMethod(getMethodName,new Class[] {});
Object value = getMethod.invoke(t, new Object[] {});
// 判断值的类型后进行强制类型转换
String textValue = null;
// if (value instanceof Integer) {
// int intValue = (Integer) value;
// cell.setCellValue(intValue);
// } else if (value instanceof Float) {
// float fValue = (Float) value;
// textValue = new HSSFRichTextString(
// String.valueOf(fValue));
// cell.setCellValue(textValue);
// } else if (value instanceof Double) {
// double dValue = (Double) value;
// textValue = new HSSFRichTextString(
// String.valueOf(dValue));
// cell.setCellValue(textValue);
// } else if (value instanceof Long) {
// long longValue = (Long) value;
// cell.setCellValue(longValue);
// }
if (value instanceof Boolean) {
boolean bValue = (Boolean) value;
textValue = "男";
if (!bValue) {
textValue = "女";
}
} else if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
textValue = sdf.format(date);
} else if (value instanceof byte[]) {
// 有图片时,设置行高为60px;
row.setHeightInPoints(60);
// 设置图片所在列宽度为80px,注意这里单位的一个换算
sheet.setColumnWidth(i, (short) (35.7 * 80));
// sheet.autoSizeColumn(i);
byte[] bsValue = (byte[]) value;
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,
1023, 255, (short) 6, index, (short) 6, index);
anchor.setAnchorType(2);
patriarch.createPicture(anchor, workbook.addPicture(
bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));
} else {
// 其它数据类型都当作字符串简单处理
if(value!=null){
textValue = value.toString();
}
}
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null) {
Pattern p = Pattern.compile("^//d+(//.//d+)?{1}quot;");
Matcher matcher = p.matcher(textValue);
if (matcher.matches()) {
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
} else {
HSSFRichTextString richString = new HSSFRichTextString(textValue);
HSSFFont font3 = workbook.createFont();
font3.setColor(HSSFColor.BLUE.index);
richString.applyFont(font3);
cell.setCellValue(richString);
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 清理资源
}
}
}
}
}
try {
workbook.write(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- Java JSP页面导出Excel
- jsp页面导出excel
- jsp页面导出excel表格
- java+jsp导出excel
- 实现导出excel表的jsp页面
- 从jsp页面中导出excel表格
- jsp页面将数据导出到Excel
- jsp页面表格数据导出到excel
- jsp页面导出成word,excel
- jsp页面将数据导出到Excel
- jsp页面导出excel,部分数据乱码
- 有关JSP页面导出EXCEL文件
- Java读取Excel内容&jsp页面内容导出到Excel中
- java jsp JXL调用模版导出Excel
- java jsp JXL调用模版导出Excel
- java通过jsp的Excel导出
- [转载]jsp页面显示数据导出到excel表中
- jsp页面显示数据导出到excel表中
- android 实现提交视频文件到服务器
- Trustzone运行环境模拟器
- Android Fragment 嵌套 destroy问题及解决
- 探索Scala(8)-- 关键字
- HDU 1873 看病要排队(优先队列的简单应用)
- Java JSP页面导出Excel
- Linux下不保存退出以及相关退出命令
- boost::typeid
- Android---如何返回上一Activity
- C++ 快速排序
- exported receiver does not require permission
- 图片提交表单
- NSTimer与ProgressView进度条的使用
- Easy UI开发问题:DataGrid组件行式填报时getEditor方法获取元素为空!