如何将jsp页面表格另存为excel格式报表,或通过打印生成.xsl报表

如何将jsp页面表格另存为excel格式报表,或通过打印生成.xsl报表,第1张

看看下面的代码,应该对你有帮助:

<%@ page import="netExcelBean" %>

<%@ page import="javaio" %>

<%@ page import="netcreateExcelCreateExcelST" %>

<%@ page import="javautil" %>

<%@ page import="comseipherdwrafterServiceDWRFinanceDWR" %>

<%@ page import="netTimeUtil" %>

<%@ page import="netNumberUtil" %>

<%@ page import="comseipherpojoafterServiceDetachCharge" %>

<%@ page import="comseipherpojosystemSetSupplyTrader" %>

<%@ page import="comseipherpojoafterServiceFinance" %>

<%@ page import="comseipherdwrafterServiceDWRAbondDWR" %>

<%@ page import="comseipherpojoafterServiceAbond" %>

<%@ page contentType="text/html;charset=utf-8" language="java" %>

<html>

<head>

<title></title>

</head>

<body>

<%

String supplyName = requestgetParameter("supplyName1");

String usersName = requestgetParameter("usersName1");

String startTimeA = requestgetParameter("startTimeA1");

String endTimeA = requestgetParameter("endTimeA1");

String pageNum = requestgetParameter("pageNum1")toString();

ExcelBean excelBean = new ExcelBean();

CreateExcelST createExcel = new CreateExcelST();

excelBeansetPath("/twoxls");

List dataResult = new ArrayList();

List titleList = new ArrayList();

if (null == pageNum) return;

if (pageNumequals("")) return;

AbondDWR abondDWR = new AbondDWR();

Map form = new HashMap();

formput("supplyName", supplyName);

formput("usersName", usersName);

formput("startTimeA", startTimeA);

formput("endTimeA", endTimeA);

formput("pageNum", pageNum);

List result = abondDWRselectAllByRows(LongparseLong(pageNum), "1", form);

List list = (ArrayList) resultget(0);

titleListadd("供应商名称");

titleListadd("供应商编码");

titleListadd("起始时间");

titleListadd("截止时间");

titleListadd("开票金额");

titleListadd(" *** 作时间");

titleListadd(" *** 作人");

titleListadd("备注");

List dataList;

for (Object o : list) {

dataList = new ArrayList();

Abond abond = (Abond) o;

dataListadd(abondgetSupplyTrader()getName());

dataListadd(abondgetSupplyTrader()getCode());

if (null != abondgetStartTime()) {

String timeA = TimeUtilgetYYYY_MM_DD(abondgetStartTime());

if (null != timeA && !timeAequals("") && !timeAequals("1970-01-01")) {

dataListadd(timeA);

} else {

dataListadd("");

}

} else dataListadd("");

if (null != abondgetEndTime()) {

String timeA = TimeUtilgetYYYY_MM_DD(abondgetEndTime());

if (null != timeA && !timeAequals("") && !timeAequals("1970-01-01")) {

dataListadd(timeA);

} else {

dataListadd("");

}

} else dataListadd("");

dataListadd(abondgetMoney() + "");

if (null != abondgetOperateTime()) {

String timeA = TimeUtilgetYYYYMMDDHHMMSSLiuPC(abondgetOperateTime());

if (null != timeA && !timeAequals("") && !timeAequals("1970-01-01")) {

dataListadd(timeA);

} else {

dataListadd("");

}

} else dataListadd("");

dataListadd(abondgetUsersManage()getName());

dataListadd(abondgetMessages());

dataResultadd(dataList);

}

int[] width = {100, 40, 40, 40, 40, 40, 40, 40};

excelBeansetReportName(startTimeA + "到" + endTimeA + "开票信息查询");

excelBeansetDataList(dataResult);

excelBeansetSheetName(startTimeA + "到" + endTimeA + "开票信息查询");

excelBeansetTitleList(titleList);

excelBeansetColumnWidth(width);

excelBeansetFlag(false);

try {

createExcelcreateExcelFile(excelBean);

} catch (Exception e) {

eprintStackTrace();

}

try {

responsesetContentType("application/x-msdownload");

responseaddHeader("Content-Disposition", "attachment;filename=" + "kaipiaoxinxi" + "xls");

FileInputStream finput = new FileInputStream(excelBeangetPath());

OutputStream output = responsegetOutputStream();

BufferedInputStream buffin = new BufferedInputStream(finput);

BufferedOutputStream buffout = new BufferedOutputStream(output);

outclear();

out = pageContextpushBody();

byte[] buffer = new byte[4096];

int count = 0;

while ((count = buffinread(buffer, 0, bufferlength)) > 0) {

buffoutwrite(buffer, 0, count);

}

buffinclose();

buffoutclose();

finputclose();

outputclose();

} catch (Exception e) {

eprintStackTrace();

} finally {

File srcExcel = new File(excelBeangetPath());

srcExceldelete();

}

%>

</body>

</html>

=============CreateExcelSTjava======================

package netcreateExcel;

import orgapachecommonsloggingLog;

import orgapachecommonsloggingLogFactory;

import orgapachepoihssfusermodel;

import orgapachepoihssfutilRegion;

import orgapachepoihssfutilHSSFColor;

import javautilList;

import javaioFileOutputStream;

import netExcelBean;

/

User: marlboro Date: 2008-2-15 Time: 10:15:38

@author Marlboro

@mailto marlboro027@gmailcom

/

public class CreateExcelST {

private Log log = LogFactorygetFactory()getInstance(thisgetClass()getName());

@SuppressWarnings("unchecked")

public void createExcelFile(ExcelBean excelBean) throws Exception {

// 创建工作薄

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet = wbcreateSheet(excelBeangetSheetName());

// 打印页面设置

HSSFPrintSetup ps = sheetgetPrintSetup();

sheetsetMargin(HSSFSheetBottomMargin, 05);// 页边距

sheetsetMargin(HSSFSheetLeftMargin, 01);

sheetsetMargin(HSSFSheetRightMargin, 01);

sheetsetMargin(HSSFSheetTopMargin, 05);

pssetLandscape(true); // 打印方向,true:横向,false:纵向

pssetPaperSize(HSSFPrintSetupA4_PAPERSIZE); // 纸张

// 设置列宽

thissetColumnWidth(sheet, excelBeangetColumnWidth());

// 标题栏设置字体

HSSFFont cellFontReport = wbcreateFont();

cellFontReportsetFontHeightInPoints((short) 16); // 字号

cellFontReportsetBoldweight(HSSFFontU_SINGLE); // 加粗

cellFontReportsetFontName("Courier New");

// 字段栏设置字体

HSSFFont cellFontColumn = wbcreateFont();

cellFontColumnsetFontHeightInPoints((short) 12); // 字号

cellFontColumnsetBoldweight(HSSFFontU_SINGLE); // 加粗

cellFontColumnsetFontName("Courier New");

cellFontColumnsetColor(HSSFFontSS_NONE);

// 设置字体

HSSFFont cellFont = wbcreateFont();

cellFontsetFontHeightInPoints((short) 10); // 字号

cellFontsetBoldweight(HSSFFontU_SINGLE); // 加粗

cellFontsetFontName("Courier New");

//自定义颜色

HSSFPalette palette = wbgetCustomPalette();

palettesetColorAtIndex(HSSFColorAQUAindex, (byte) 252, (byte) 254, (byte) 236);

// 设置标题栏单元格格式

HSSFCellStyle cellStyleReport = wbcreateCellStyle();

cellStyleReportsetFont(cellFontReport);

cellStyleReportsetAlignment(HSSFCellStyleALIGN_CENTER); // 左右居中

cellStyleReportsetVerticalAlignment(HSSFCellStyleVERTICAL_CENTER); // 上下居中

cellStyleReportsetBorderBottom(HSSFCellStyleBORDER_THIN); // 下边框

cellStyleReportsetBorderLeft(HSSFCellStyleBORDER_THIN); // 左边框

cellStyleReportsetBorderRight(HSSFCellStyleBORDER_THIN); // 右边框

cellStyleReportsetBorderTop(HSSFCellStyleBORDER_THIN); // 上边框

cellStyleReportsetWrapText(true);// 自动换行

cellStyleReportsetFillForegroundColor(HSSFColorAQUAindex);

cellStyleReportsetFillPattern(HSSFCellStyleSOLID_FOREGROUND);

// 设置标题栏单元格格式

HSSFCellStyle cellStyleColumn = wbcreateCellStyle();

cellStyleColumnsetFont(cellFontColumn);

cellStyleColumnsetAlignment(HSSFCellStyleALIGN_CENTER); // 左右居中

cellStyleColumnsetVerticalAlignment(HSSFCellStyleVERTICAL_CENTER); // 上下居中

cellStyleColumnsetBorderBottom(HSSFCellStyleBORDER_THIN); // 下边框

cellStyleColumnsetBorderLeft(HSSFCellStyleBORDER_THIN); // 左边框

cellStyleColumnsetBorderRight(HSSFCellStyleBORDER_THIN); // 右边框

cellStyleColumnsetBorderTop(HSSFCellStyleBORDER_THIN); // 上边框

cellStyleColumnsetWrapText(true);// 自动换行

cellStyleColumnsetFillForegroundColor(HSSFColorGREY_25_PERCENTindex);

cellStyleColumnsetFillPattern(HSSFCellStyleSOLID_FOREGROUND);

// 设置单元格格式

HSSFCellStyle cellStyle = wbcreateCellStyle();

cellStylesetFont(cellFont);

cellStylesetAlignment(HSSFCellStyleALIGN_CENTER); // 左右居中

cellStylesetVerticalAlignment(HSSFCellStyleVERTICAL_CENTER); // 上下居中

cellStylesetBorderBottom(HSSFCellStyleBORDER_THIN); // 下边框

cellStylesetBorderLeft(HSSFCellStyleBORDER_THIN); // 左边框

cellStylesetBorderRight(HSSFCellStyleBORDER_THIN); // 右边框

cellStylesetBorderTop(HSSFCellStyleBORDER_THIN); // 上边框

cellStylesetWrapText(true);// 自动换行

List dataList = excelBeangetDataList();

if (null != dataList) {

// 报表的标题

List titleList = excelBeangetTitleList();

// 创建行 设置报表名称

HSSFRow rowReportName = sheetcreateRow(0);

HSSFCell cellReportName = rowReportNamecreateCell((short) 0);

sheetaddMergedRegion(new Region(0, (short) 0, 0, (short) (titleListsize() - 1)));

HSSFRichTextString reportString = new HSSFRichTextString(excelBeangetReportName());

cellReportNamesetCellValue(reportString);

cellReportNamesetCellStyle(cellStyleReport);

HSSFCell endReportName = rowReportNamecreateCell((short) (titleListsize() - 1));

endReportNamesetCellStyle(cellStyleReport);

// 创建行

HSSFRow rowTitle = sheetcreateRow(1);

for (int i = 0; i < titleListsize(); i++) {

String s_title = (String) titleListget(i);

HSSFCell cellx_y = rowTitlecreateCell((short) i);

HSSFRichTextString hssfRichTextString = new HSSFRichTextString(s_title);

// 单元格内容

cellx_ysetCellValue(hssfRichTextString);

// 单元格格式

cellx_ysetCellStyle(cellStyleColumn);

}

// 报表数据

for (int i = 0; i < dataListsize(); i++) {

HSSFRow row = sheetcreateRow(i + 2);

List list_row = (List) dataListget(i);

for (int j = 0; j < list_rowsize(); j++) {

String strtmp = (String) list_rowget(j);

// String strtmp = list_rowget(j)toString();

if (" "equals(strtmp)) {

strtmp = " ";

}

HSSFCell cell = rowcreateCell((short) j);

HSSFRichTextString hssfRichTextString = new HSSFRichTextString(strtmp);

cellsetCellValue(hssfRichTextString);

cellsetCellStyle(cellStyle);

}

}

}

// 另外一个标题

List otherTitleList = excelBeangetOtherTitleList();

if (otherTitleList != null) {

// 创建行

HSSFRow otherRowTitle = sheetcreateRow(dataListsize() + 2);

for (int i = 0; i < otherTitleListsize(); i++) {

String s_title = (String) otherTitleListget(i);

HSSFCell cellx_y = otherRowTitlecreateCell((short) i);

HSSFRichTextString hssfRichTextString = new HSSFRichTextString(s_title);

cellx_ysetCellValue(hssfRichTextString); // 单元格内容

cellx_ysetCellStyle(cellStyle); // 单元格格式

}

// 另外的数据集

List otherDataList = excelBeangetOtherDataList();

// 报表数据

for (int i = 0; i < otherDataListsize(); i++) {

HSSFRow row = sheetcreateRow(dataListsize() + 3 + i);

List list_row = (List) otherDataListget(i);

for (int j = 0; j < list_rowsize(); j++) {

String strtmp = (String) list_rowget(j);

if (" "equals(strtmp)) {

strtmp = " ";

}

HSSFCell cell = rowcreateCell((short) j);

HSSFRichTextString hssfRichTextString = new HSSFRichTextString(strtmp);

cellsetCellValue(hssfRichTextString);

cellsetCellStyle(cellStyle);

}

}

}

try {

FileOutputStream fileOut = new FileOutputStream(excelBeangetPath());

logdebug("===FilePath>>>>>>>>>>>>>>>>>===>>>>>>" + excelBeangetPath());

wbwrite(fileOut);

fileOutclose();

} catch (Exception e) {

throw new Exception("文件已经打开,请关闭后再生成");

}

}

public void setColumnWidth(HSSFSheet sheet, int[] width) {

for (int i = 0; i < widthlength; i++) {

sheetsetColumnWidth((short) i, (short) (width[i] 100));

}

}

}

1、把数据库建好

CREATE TABLE `User` (

`id` int(11) NOT NULL,

`username` varchar(255) DEFAULT NULL,

`password` varchar(255) DEFAULT NULL,

`address` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

)

2、插入一个数据

insert into User values(1,"Martin","12345","zjut")

3、建立一个Dynamic Web Project,目录如下:

4、showinfojsp,这里需要注意的是导入类:import="comentityUser"

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8" import="comentityUser"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 401 Transitional//EN" ">

<html>

<head>

<meta >

<title>Insert title here</title>

</head>

<body>

<%

User user = (User)requestgetAttribute("user");

%>

(此处空一行)

id:<%=usergetId() %>

username:<%=usergetUsername() %>

password:<%=usergetPassword() %>

address:<%=usergetAddress() %>

(此处空一行)

<!--

也可以用el表达式,但是同样需要servlet先将值存放在request中,但是用el表达式的话需要在上面加上:

<%@ taglib uri=">

如下,获取id这样写就可以

${usergetId()}

-->

</body>

</html>

5、结果如下:

1、在后台定义一个Servlet或者Action,接收jsp的参数去数据库查询数据,返回List

2、将从数据库查询的数据放在request中,如requestsetAttribute("studentList",studentList),输出到对应的jsp页面。

3、在jsp页面引入jstl标签,定义好一个html表格头

4、用jstl获取后台查询的数据,利用<c:foreach>标签循环输出到表格的<tr>中。

示例:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>

<body>

    <table class="table table-bordered" >

<tr>

<th style="text-align:center;width:2%">序号</th>

<th style="text-align:center;width:5%">姓名</th>

<th style="text-align:center;width:6%">年龄</th>

</tr>

<c:forEach items="${studentList}" var="student" varStatus="status">

<tr>

    <td style="text-align:center;">${status}</td>

    <td style="text-align:center;">${studentusername}</td>

    <td style="text-align:center;">${studentage}</td>

</tr>

</c:forEach>

</table>

 </body>

</html>

可以做一个jsp页面,然后在这上面生成表格数据

如下代码即可实现打印<html<head<title打印</title

<script language=javascript

function doPrint(){windowprint();}<script</head

这里是由jsp生成的数据表格,不过注意排版</body</html

我用的strtus2 把Action中的代码贴一下 这个简单的示例可以参考下

package comaneelxdhaction;

import javaioByteArrayInputStream;

import javaioByteArrayOutputStream;

import javaioIOException;

import javaioInputStream;

import javautilArrayList;

import javautilList;

import javaxservlet>

以上就是关于如何将jsp页面表格另存为excel格式报表,或通过打印生成.xsl报表全部的内容,包括:如何将jsp页面表格另存为excel格式报表,或通过打印生成.xsl报表、jsp中如何把数据库中查询出来的数据输出到jsp页面。、JSP执行数据库查询,然后将查询结果用html表格的形式显示出来等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10200798.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存