如何将jsp数据导出到excel中

我的页面数据是通过jqueryjson得到生成的表格我想将表格的数据,在点击一个按钮后弹出对话框选择路径和名字后。导出大excel中如果有知道的告诉我一下,最好带着示例我... 我的页面数据是通过jquery json得到生成的表格我想将表格的数据,在点击一个按钮后弹出对话框选择路径和名字后。导出大excel中如果有知道的告诉我一下,最好带着示例我会加分的 展开
 我来答
植下琪大教
2010-05-06 · TA获得超过217个赞
知道小有建树答主
回答量:121
采纳率:0%
帮助的人:78.3万
展开全部
看看下面的代码,应该对你有帮助:

<%@ page import="net.ExcelBean" %>
<%@ page import="java.io.*" %>
<%@ page import="net.createExcel.CreateExcelST" %>
<%@ page import="java.util.*" %>
<%@ page import="com.seipher.dwr.afterServiceDWR.FinanceDWR" %>
<%@ page import="net.TimeUtil" %>
<%@ page import="net.NumberUtil" %>
<%@ page import="com.seipher.pojo.afterService.DetachCharge" %>
<%@ page import="com.seipher.pojo.systemSet.SupplyTrader" %>
<%@ page import="com.seipher.pojo.afterService.Finance" %>
<%@ page import="com.seipher.dwr.afterServiceDWR.AbondDWR" %>
<%@ page import="com.seipher.pojo.afterService.Abond" %>
<%@ page contentType="text/html;charset=utf-8" language="java" %>

<html>
<head>
<title></title>
</head>
<body>

<%
String supplyName = request.getParameter("supplyName1");
String usersName = request.getParameter("usersName1");
String startTimeA = request.getParameter("startTimeA1");
String endTimeA = request.getParameter("endTimeA1");
String pageNum = request.getParameter("pageNum1").toString();
ExcelBean excelBean = new ExcelBean();
CreateExcelST createExcel = new CreateExcelST();
excelBean.setPath("/two.xls");
List dataResult = new ArrayList();
List titleList = new ArrayList();

if (null == pageNum) return;
if (pageNum.equals("")) return;
AbondDWR abondDWR = new AbondDWR();
Map form = new HashMap();
form.put("supplyName", supplyName);
form.put("usersName", usersName);
form.put("startTimeA", startTimeA);
form.put("endTimeA", endTimeA);
form.put("pageNum", pageNum);
List result = abondDWR.selectAllByRows(Long.parseLong(pageNum), "1", form);
List list = (ArrayList) result.get(0);
titleList.add("供应商名称");
titleList.add("供应商编码");
titleList.add("起始时间");
titleList.add("截止时间");
titleList.add("开票金额");
titleList.add("操作时间");
titleList.add("操作人");
titleList.add("备注");

List dataList;
for (Object o : list) {
dataList = new ArrayList();
Abond abond = (Abond) o;
dataList.add(abond.getSupplyTrader().getName());
dataList.add(abond.getSupplyTrader().getCode());
if (null != abond.getStartTime()) {
String timeA = TimeUtil.getYYYY_MM_DD(abond.getStartTime());
if (null != timeA && !timeA.equals("") && !timeA.equals("1970-01-01")) {
dataList.add(timeA);
} else {
dataList.add("");
}
} else dataList.add("");
if (null != abond.getEndTime()) {
String timeA = TimeUtil.getYYYY_MM_DD(abond.getEndTime());
if (null != timeA && !timeA.equals("") && !timeA.equals("1970-01-01")) {
dataList.add(timeA);
} else {
dataList.add("");
}
} else dataList.add("");
dataList.add(abond.getMoney() + "");
if (null != abond.getOperateTime()) {
String timeA = TimeUtil.getYYYYMMDDHHMMSSLiuPC(abond.getOperateTime());
if (null != timeA && !timeA.equals("") && !timeA.equals("1970-01-01")) {
dataList.add(timeA);
} else {
dataList.add("");
}
} else dataList.add("");
dataList.add(abond.getUsersManage().getName());
dataList.add(abond.getMessages());
dataResult.add(dataList);
}
int[] width = {100, 40, 40, 40, 40, 40, 40, 40};
excelBean.setReportName(startTimeA + "到" + endTimeA + "开票信息查询");
excelBean.setDataList(dataResult);
excelBean.setSheetName(startTimeA + "到" + endTimeA + "开票信息查询");
excelBean.setTitleList(titleList);
excelBean.setColumnWidth(width);
excelBean.setFlag(false);
try {
createExcel.createExcelFile(excelBean);
} catch (Exception e) {
e.printStackTrace();
}
try {
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename=" + "kaipiaoxinxi" + ".xls");
FileInputStream finput = new FileInputStream(excelBean.getPath());

OutputStream output = response.getOutputStream();
BufferedInputStream buffin = new BufferedInputStream(finput);
BufferedOutputStream buffout = new BufferedOutputStream(output);
out.clear();
out = pageContext.pushBody();
byte[] buffer = new byte[4096];
int count = 0;
while ((count = buffin.read(buffer, 0, buffer.length)) > 0) {
buffout.write(buffer, 0, count);
}
buffin.close();
buffout.close();
finput.close();
output.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
File srcExcel = new File(excelBean.getPath());
srcExcel.delete();
}
%>
</body>
</html>

=============CreateExcelST.java======================
package net.createExcel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.hssf.util.HSSFColor;

import java.util.List;
import java.io.FileOutputStream;

import net.ExcelBean;

/**
* User: marlboro Date: 2008-2-15 Time: 10:15:38
*
* @author Marlboro
* @mailto marlboro027@gmail.com
*/
public class CreateExcelST {
private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());

@SuppressWarnings("unchecked")
public void createExcelFile(ExcelBean excelBean) throws Exception {
// 创建工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(excelBean.getSheetName());

// 打印页面设置
HSSFPrintSetup ps = sheet.getPrintSetup();
sheet.setMargin(HSSFSheet.BottomMargin, 0.5);// 页边距
sheet.setMargin(HSSFSheet.LeftMargin, 0.1);
sheet.setMargin(HSSFSheet.RightMargin, 0.1);
sheet.setMargin(HSSFSheet.TopMargin, 0.5);
ps.setLandscape(true); // 打印方向,true:横向,false:纵向
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); // 纸张

// 设置列宽
this.setColumnWidth(sheet, excelBean.getColumnWidth());

// 标题栏设置字体
HSSFFont cellFontReport = wb.createFont();
cellFontReport.setFontHeightInPoints((short) 16); // 字号
cellFontReport.setBoldweight(HSSFFont.U_SINGLE); // 加粗
cellFontReport.setFontName("Courier New");
// 字段栏设置字体
HSSFFont cellFontColumn = wb.createFont();
cellFontColumn.setFontHeightInPoints((short) 12); // 字号
cellFontColumn.setBoldweight(HSSFFont.U_SINGLE); // 加粗
cellFontColumn.setFontName("Courier New");
cellFontColumn.setColor(HSSFFont.SS_NONE);
// 设置字体
HSSFFont cellFont = wb.createFont();
cellFont.setFontHeightInPoints((short) 10); // 字号
cellFont.setBoldweight(HSSFFont.U_SINGLE); // 加粗
cellFont.setFontName("Courier New");

//自定义颜色
HSSFPalette palette = wb.getCustomPalette();
palette.setColorAtIndex(HSSFColor.AQUA.index, (byte) 252, (byte) 254, (byte) 236);

// 设置标题栏单元格格式
HSSFCellStyle cellStyleReport = wb.createCellStyle();
cellStyleReport.setFont(cellFontReport);
cellStyleReport.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyleReport.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
cellStyleReport.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
cellStyleReport.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框
cellStyleReport.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框
cellStyleReport.setBorderTop(HSSFCellStyle.BORDER_THIN); // 上边框
cellStyleReport.setWrapText(true);// 自动换行
cellStyleReport.setFillForegroundColor(HSSFColor.AQUA.index);
cellStyleReport.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 设置标题栏单元格格式
HSSFCellStyle cellStyleColumn = wb.createCellStyle();
cellStyleColumn.setFont(cellFontColumn);
cellStyleColumn.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyleColumn.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
cellStyleColumn.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
cellStyleColumn.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框
cellStyleColumn.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框
cellStyleColumn.setBorderTop(HSSFCellStyle.BORDER_THIN); // 上边框
cellStyleColumn.setWrapText(true);// 自动换行
cellStyleColumn.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cellStyleColumn.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

// 设置单元格格式
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(cellFont);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 上边框
cellStyle.setWrapText(true);// 自动换行

List dataList = excelBean.getDataList();
if (null != dataList) {
// 报表的标题
List titleList = excelBean.getTitleList();

// 创建行 设置报表名称
HSSFRow rowReportName = sheet.createRow(0);
HSSFCell cellReportName = rowReportName.createCell((short) 0);
sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) (titleList.size() - 1)));
HSSFRichTextString reportString = new HSSFRichTextString(excelBean.getReportName());
cellReportName.setCellValue(reportString);
cellReportName.setCellStyle(cellStyleReport);
HSSFCell endReportName = rowReportName.createCell((short) (titleList.size() - 1));
endReportName.setCellStyle(cellStyleReport);
// 创建行
HSSFRow rowTitle = sheet.createRow(1);

for (int i = 0; i < titleList.size(); i++) {
String s_title = (String) titleList.get(i);
HSSFCell cellx_y = rowTitle.createCell((short) i);
HSSFRichTextString hssfRichTextString = new HSSFRichTextString(s_title);
// 单元格内容
cellx_y.setCellValue(hssfRichTextString);
// 单元格格式
cellx_y.setCellStyle(cellStyleColumn);
}

// 报表数据
for (int i = 0; i < dataList.size(); i++) {
HSSFRow row = sheet.createRow(i + 2);
List list_row = (List) dataList.get(i);
for (int j = 0; j < list_row.size(); j++) {
String strtmp = (String) list_row.get(j);
// String strtmp = list_row.get(j).toString();
if (" ".equals(strtmp)) {
strtmp = " ";
}
HSSFCell cell = row.createCell((short) j);
HSSFRichTextString hssfRichTextString = new HSSFRichTextString(strtmp);
cell.setCellValue(hssfRichTextString);
cell.setCellStyle(cellStyle);
}
}
}

// 另外一个标题
List otherTitleList = excelBean.getOtherTitleList();
if (otherTitleList != null) {
// 创建行
HSSFRow otherRowTitle = sheet.createRow(dataList.size() + 2);

for (int i = 0; i < otherTitleList.size(); i++) {
String s_title = (String) otherTitleList.get(i);
HSSFCell cellx_y = otherRowTitle.createCell((short) i);
HSSFRichTextString hssfRichTextString = new HSSFRichTextString(s_title);
cellx_y.setCellValue(hssfRichTextString); // 单元格内容
cellx_y.setCellStyle(cellStyle); // 单元格格式
}

// 另外的数据集
List otherDataList = excelBean.getOtherDataList();

// 报表数据
for (int i = 0; i < otherDataList.size(); i++) {
HSSFRow row = sheet.createRow(dataList.size() + 3 + i);
List list_row = (List) otherDataList.get(i);
for (int j = 0; j < list_row.size(); j++) {
String strtmp = (String) list_row.get(j);
if (" ".equals(strtmp)) {
strtmp = " ";
}
HSSFCell cell = row.createCell((short) j);
HSSFRichTextString hssfRichTextString = new HSSFRichTextString(strtmp);
cell.setCellValue(hssfRichTextString);
cell.setCellStyle(cellStyle);
}
}
}

try {
FileOutputStream fileOut = new FileOutputStream(excelBean.getPath());
log.debug("===FilePath>>>>>>>>>>>>>>>>>.===>>>>>>" + excelBean.getPath());
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
throw new Exception("文件已经打开,请关闭后再生成");
}
}

public void setColumnWidth(HSSFSheet sheet, int[] width) {
for (int i = 0; i < width.length; i++) {
sheet.setColumnWidth((short) i, (short) (width[i] * 100));
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式