ssh框架 web页面导出excel文件,弹出窗怎么实现?
现在是直接写死了路径,点击页面上的导出以后,直接在D盘根路径生成导出的文件,怎样做成:点击导出跳出下载框,选择下载地址?网上搜的response.getOutputStr...
现在是直接写死了路径,点击页面上的导出以后,直接在D盘根路径生成导出的文件,怎样做成:
点击导出跳出下载框,选择下载地址?
网上搜的response.getOutputStream();方法response没法用~谢谢了 展开
点击导出跳出下载框,选择下载地址?
网上搜的response.getOutputStream();方法response没法用~谢谢了 展开
推荐于2016-08-20 · 知道合伙人教育行家
关注
展开全部
实现方法:
1、打开ssh编辑器
2、输入以下代码:
<input class="see" type="button" value="导出Excel" onclick="exportxls()"/>
function exportxls()
{
window.location = "goodsReport_exportxls.action";
}
struts.xml中的配置
<constant name="struts.multipart.parser" value="jakarta" />
<result name="exportxls" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">inputStream</param>
</result>
action的代码
public String exportxls()
{
try
{
Workbook workbook = new HSSFWorkbook();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.VERTICAL_CENTER);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font);
3、保存退出
1、打开ssh编辑器
2、输入以下代码:
<input class="see" type="button" value="导出Excel" onclick="exportxls()"/>
function exportxls()
{
window.location = "goodsReport_exportxls.action";
}
struts.xml中的配置
<constant name="struts.multipart.parser" value="jakarta" />
<result name="exportxls" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">inputStream</param>
</result>
action的代码
public String exportxls()
{
try
{
Workbook workbook = new HSSFWorkbook();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.VERTICAL_CENTER);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font);
3、保存退出
展开全部
struts框架有上传下载功能,直接框架,会出现选择路径的弹出框
追问
怎么实现?是写在JSP上还是后台代码?新手最好能给个例子
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<input class="see" type="button" value="导出Excel" onclick="exportxls()"/>
function exportxls()
{
window.location = "goodsReport_exportxls.action";
}
struts.xml中的配置
<constant name="struts.multipart.parser" value="jakarta" />
<result name="exportxls" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">inputStream</param>
</result>
action的代码
public String exportxls()
{
try
{
Workbook workbook = new HSSFWorkbook();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.VERTICAL_CENTER);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font);
Sheet sheet = workbook.createSheet("物品报表");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("序号");
cell.setCellStyle(style);
Cell cell1 = row.createCell(1);
cell1.setCellValue("品名");
cell1.setCellStyle(style);
Cell cell2 = row.createCell(2);
cell2.setCellValue("学生姓名");
cell2.setCellStyle(style);
Cell cell3 = row.createCell(3);
cell3.setCellValue("价格");
cell3.setCellStyle(style);
Cell cell4 = row.createCell(4);
cell4.setCellValue("时间");
cell4.setCellStyle(style);
List<String[]> datas = goodsReportService.getDatas();
int size = datas.size();
for (int i = 0; i < size; i++)
{
String[] e = datas.get(i);
Row rowc = sheet.createRow(i+1);
Cell cell0 = rowc.createCell(0);
cell0.setCellValue(i + 1);
Cell cellc1 = rowc.createCell(1);
cellc1.setCellValue(e[0]);
Cell cellc2 = rowc.createCell(2);
cellc2.setCellValue(e[1]);
Cell cellc3 = rowc.createCell(3);
cellc3.setCellValue(e[2]);
Cell cellc4 = rowc.createCell(4);
cellc4.setCellValue(e[3]);
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String name = sdf.format(new Date()) + "物品报表.xls";
name = java.net.URLEncoder.encode(name, "UTF-8");
fileName = new String(name.getBytes(), "iso-8859-1");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
outputStream.flush();
byte[] byteArray = outputStream.toByteArray();
inputStream = new ByteArrayInputStream(byteArray, 0, byteArray.length);
outputStream.close();
}
catch(Exception e)
{
}
return "exportxls";
}
function exportxls()
{
window.location = "goodsReport_exportxls.action";
}
struts.xml中的配置
<constant name="struts.multipart.parser" value="jakarta" />
<result name="exportxls" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">inputStream</param>
</result>
action的代码
public String exportxls()
{
try
{
Workbook workbook = new HSSFWorkbook();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.VERTICAL_CENTER);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font);
Sheet sheet = workbook.createSheet("物品报表");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("序号");
cell.setCellStyle(style);
Cell cell1 = row.createCell(1);
cell1.setCellValue("品名");
cell1.setCellStyle(style);
Cell cell2 = row.createCell(2);
cell2.setCellValue("学生姓名");
cell2.setCellStyle(style);
Cell cell3 = row.createCell(3);
cell3.setCellValue("价格");
cell3.setCellStyle(style);
Cell cell4 = row.createCell(4);
cell4.setCellValue("时间");
cell4.setCellStyle(style);
List<String[]> datas = goodsReportService.getDatas();
int size = datas.size();
for (int i = 0; i < size; i++)
{
String[] e = datas.get(i);
Row rowc = sheet.createRow(i+1);
Cell cell0 = rowc.createCell(0);
cell0.setCellValue(i + 1);
Cell cellc1 = rowc.createCell(1);
cellc1.setCellValue(e[0]);
Cell cellc2 = rowc.createCell(2);
cellc2.setCellValue(e[1]);
Cell cellc3 = rowc.createCell(3);
cellc3.setCellValue(e[2]);
Cell cellc4 = rowc.createCell(4);
cellc4.setCellValue(e[3]);
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String name = sdf.format(new Date()) + "物品报表.xls";
name = java.net.URLEncoder.encode(name, "UTF-8");
fileName = new String(name.getBytes(), "iso-8859-1");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
outputStream.flush();
byte[] byteArray = outputStream.toByteArray();
inputStream = new ByteArrayInputStream(byteArray, 0, byteArray.length);
outputStream.close();
}
catch(Exception e)
{
}
return "exportxls";
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询