如何将jsp页面表格另存为excel格式报表,或通过打印生成.xsl报表
7个回答
展开全部
看看下面的代码,应该对你有帮助:
<%@ 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));
}
}
}
<%@ 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));
}
}
}
展开全部
js方法,加个按钮调用就好了。
function toExcel() {
try {
var oXL = new ActiveXObject("Excel.Application");
}catch(e) {
alert( "要将该表导出到Excel,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。");
return "";
}
var oWB = oXL.Workbooks.Add(); //新增工作簿
var oSheet = oWB.ActiveSheet; //创建工作表
var sel=document.body.createTextRange();
sel.moveToElementText(dataTable);//dataTable为table的id
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true; //设置excel为可见
oXL.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制
oSheet = null;
oWB = null;
oXL = null;
}
function toExcel() {
try {
var oXL = new ActiveXObject("Excel.Application");
}catch(e) {
alert( "要将该表导出到Excel,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。");
return "";
}
var oWB = oXL.Workbooks.Add(); //新增工作簿
var oSheet = oWB.ActiveSheet; //创建工作表
var sel=document.body.createTextRange();
sel.moveToElementText(dataTable);//dataTable为table的id
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true; //设置excel为可见
oXL.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制
oSheet = null;
oWB = null;
oXL = null;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-01-24 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
jsp页面的表格数据可以另存为excel表格的。
JSP从数据库导出数据到Excel下载的实现
关键代码:
<%@ page contentType="application/msexcel" %>
<%
//response.setHeader("Content-disposition","inline; filename=videos.xls");
response.setHeader("Content-disposition","attachment; filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%>
<%@ page contentType="application/msexcel" %>
<%
//response.setHeader("Content-disposition","inline; filename=videos.xls");
response.setHeader("Content-disposition","attachment; filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@ page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page import="com.test.*"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UserManager um = (UserManager) ctx.getBean("userManager");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>spring jdbc test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<table border="1" width="100%">
<tr> <td>id</td> <td>name</td>
</tr>
<%
List<User> users2=um.getUserList();
for(int i=0;i<users2.size();i++)
{
int t_id2=users2.get(i).getId();
String t_name2=users2.get(i).getName();
%>
<tr>
<td><%=t_id2 %></td> <td><%=t_name2 %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
JSP从数据库导出数据到Excel下载的实现
关键代码:
<%@ page contentType="application/msexcel" %>
<%
//response.setHeader("Content-disposition","inline; filename=videos.xls");
response.setHeader("Content-disposition","attachment; filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%>
<%@ page contentType="application/msexcel" %>
<%
//response.setHeader("Content-disposition","inline; filename=videos.xls");
response.setHeader("Content-disposition","attachment; filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@ page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page import="com.test.*"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UserManager um = (UserManager) ctx.getBean("userManager");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>spring jdbc test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<table border="1" width="100%">
<tr> <td>id</td> <td>name</td>
</tr>
<%
List<User> users2=um.getUserList();
for(int i=0;i<users2.size();i++)
{
int t_id2=users2.get(i).getId();
String t_name2=users2.get(i).getName();
%>
<tr>
<td><%=t_id2 %></td> <td><%=t_name2 %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
jsp页面的表格数据可以另存为excel表格的。
JSP从数据库导出数据到Excel下载的实现
关键代码:
<%@
page
contentType="application/msexcel"
%>
<%
//response.setHeader("Content-disposition","inline;
filename=videos.xls");
response.setHeader("Content-disposition","attachment;
filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@
page
language="java"
import="java.util.*,java.io.*"
pageEncoding="GBK"%>
<%@
page
contentType="application/msexcel"
%>
<%
//response.setHeader("Content-disposition","inline;
filename=videos.xls");
response.setHeader("Content-disposition","attachment;
filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@
page
import="org.springframework.web.context.WebApplicationContext"%>
<%@
page
import="com.test.*"%>
<%@
page
import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
String
path
=
request.getContextPath();
String
basePath
=
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
WebApplicationContext
ctx
=
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UserManager
um
=
(UserManager)
ctx.getBean("userManager");
%>
<!DOCTYPE
HTML
PUBLIC
"-//W3C//DTD
HTML
4.01
Transitional//EN">
<html>
<head>
<base
href="<%=basePath%>">
<title>spring
jdbc
test</title>
<meta
http-equiv="pragma"
content="no-cache">
<meta
http-equiv="cache-control"
content="no-cache">
<meta
http-equiv="expires"
content="0">
<meta
http-equiv="keywords"
content="keyword1,keyword2,keyword3">
<meta
http-equiv="description"
content="This
is
my
page">
<!--
<link
rel="stylesheet"
type="text/css"
href="styles.css">
-->
</head>
<body>
<br>
<table
border="1"
width="100%">
<tr>
<td>id</td>
<td>name</td>
</tr>
<%
List<User>
users2=um.getUserList();
for(int
i=0;i<users2.size();i++)
{
int
t_id2=users2.get(i).getId();
String
t_name2=users2.get(i).getName();
%>
<tr>
<td><%=t_id2
%></td>
<td><%=t_name2
%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
JSP从数据库导出数据到Excel下载的实现
关键代码:
<%@
page
contentType="application/msexcel"
%>
<%
//response.setHeader("Content-disposition","inline;
filename=videos.xls");
response.setHeader("Content-disposition","attachment;
filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@
page
language="java"
import="java.util.*,java.io.*"
pageEncoding="GBK"%>
<%@
page
contentType="application/msexcel"
%>
<%
//response.setHeader("Content-disposition","inline;
filename=videos.xls");
response.setHeader("Content-disposition","attachment;
filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@
page
import="org.springframework.web.context.WebApplicationContext"%>
<%@
page
import="com.test.*"%>
<%@
page
import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
String
path
=
request.getContextPath();
String
basePath
=
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
WebApplicationContext
ctx
=
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UserManager
um
=
(UserManager)
ctx.getBean("userManager");
%>
<!DOCTYPE
HTML
PUBLIC
"-//W3C//DTD
HTML
4.01
Transitional//EN">
<html>
<head>
<base
href="<%=basePath%>">
<title>spring
jdbc
test</title>
<meta
http-equiv="pragma"
content="no-cache">
<meta
http-equiv="cache-control"
content="no-cache">
<meta
http-equiv="expires"
content="0">
<meta
http-equiv="keywords"
content="keyword1,keyword2,keyword3">
<meta
http-equiv="description"
content="This
is
my
page">
<!--
<link
rel="stylesheet"
type="text/css"
href="styles.css">
-->
</head>
<body>
<br>
<table
border="1"
width="100%">
<tr>
<td>id</td>
<td>name</td>
</tr>
<%
List<User>
users2=um.getUserList();
for(int
i=0;i<users2.size();i++)
{
int
t_id2=users2.get(i).getId();
String
t_name2=users2.get(i).getName();
%>
<tr>
<td><%=t_id2
%></td>
<td><%=t_name2
%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用POI插件。
去网上下一个poi-bin.tar.gz,把里面的4个jar导入到你的JAVA项目中,就可以调用已经定义好的类和函数来导出导出xsl。代码网上也有,在这里就不贴出来了。
去网上下一个poi-bin.tar.gz,把里面的4个jar导入到你的JAVA项目中,就可以调用已经定义好的类和函数来导出导出xsl。代码网上也有,在这里就不贴出来了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |