如何将jsp 中的数据导入到excel表格 中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将jsp 中的数据导入到excel表格 中相关的知识,希望对你有一定的参考价值。

将从数据库中查询出来的数据,显示在jsp 页面中, 想通过点击一个按钮,将显示在jsp 页面中的数据导入到excel 表格中

可以用servlet实现跳转redirectnameExcel.xls就行了。

//拼凑excel文件名字
String filename = String.valueOf(year)+String.valueOf(month)+String.valueOf(day)+h+mise+".xls";
String path = getServletContext().getRealPath("excelexport");
System.out.println("path:"+path);
try
FileOutputStream fos = new FileOutputStream(path+"/"+filename);

// 创建新的Excel 工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 在Excel 工作簿中建一工作表
HSSFSheet s = wb.createSheet();
String sheetName = year+"-"+month+"-"+day;
wb.setSheetName(0, sheetName);
//在索引0的位置创建行(第一行)
HSSFRow row = s.createRow((short)0);
HSSFCell cell0 = row.createCell((short) 0);// 第一列
HSSFCell cell1 = row.createCell((short) 1);
HSSFCell cell2 = row.createCell((short) 2);
HSSFCell cell3 = row.createCell((short) 3);
HSSFCell cell4 = row.createCell((short) 4);
HSSFCell cell5 = row.createCell((short) 5);
HSSFCell cell6 = row.createCell((short) 6);
// 定义单元格为字符串类型
cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
cell6.setCellType(HSSFCell.CELL_TYPE_STRING);

cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
// 在单元格中输入数据
cell0.setCellValue("科室名");
cell1.setCellValue("版组");
cell2.setCellValue("注册名");
cell3.setCellValue("问题题目");
cell4.setCellValue("问题内容");
cell5.setCellValue("发表时间");
cell6.setCellValue("Status");
KSuser ks =new KSuser();

HSSFRow[] rows = null;
if(list != null && list.size()>0)
rows = new HSSFRow[list.size()];

int j = 1;
for(int k=0;k <list.size();k++)
ks =list.get(k);
//设置行从第二行开始
rows[j-1] =s.createRow((short)(j));
String[] str =new String[7];
str[0]=ks.getKeshi();
str[1]=ks.getBanzu();
str[2]=ks.getReg_name();
str[3]=ks.getSubject();
str[4]=ks.getText();
str[5]=ks.getRe_day().toString();
str[6]=ks.getStatus();
for(int i=1;i <8;i++)
HSSFCell cell =rows[j-1].createCell((short)(i-1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(str[i-1]);

j++;


wb.write(fos);
fos.close();
catch(Exception e)
e.printStackTrace();
out.println(" <script>alert('异常操作,文件可能正保护中\\n请重试!');history.go(-1); </script>");

String urlname = request.getRequestURI();
urlname = urlname.replaceAll("excel", "excelexport");
urlname = urlname+"/"+filename;
System.out.println("urlname:"+urlname);
//request.getSession().setAttribute("excelfile", urlname);
response.sendRedirect(urlname);
参考技术A 可以用servlet实现跳转redirectnameExcel.xls就行了。

//拼凑excel文件名字
String filename = String.valueOf(year)+String.valueOf(month)+String.valueOf(day)+h+mise+".xls";
String path = getServletContext().getRealPath("excelexport");
System.out.println("path:"+path);
try
FileOutputStream fos = new FileOutputStream(path+"/"+filename);

// 创建新的Excel 工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 在Excel 工作簿中建一工作表
HSSFSheet s = wb.createSheet();
String sheetName = year+"-"+month+"-"+day;
wb.setSheetName(0, sheetName);
//在索引0的位置创建行(第一行)
HSSFRow row = s.createRow((short)0);
HSSFCell cell0 = row.createCell((short) 0);// 第一列
HSSFCell cell1 = row.createCell((short) 1);
HSSFCell cell2 = row.createCell((short) 2);
HSSFCell cell3 = row.createCell((short) 3);
HSSFCell cell4 = row.createCell((short) 4);
HSSFCell cell5 = row.createCell((short) 5);
HSSFCell cell6 = row.createCell((short) 6);
// 定义单元格为字符串类型
cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
cell6.setCellType(HSSFCell.CELL_TYPE_STRING);

cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
// 在单元格中输入数据
cell0.setCellValue("科室名");
cell1.setCellValue("版组");
cell2.setCellValue("注册名");
cell3.setCellValue("问题题目");
cell4.setCellValue("问题内容");
cell5.setCellValue("发表时间");
cell6.setCellValue("Status");
KSuser ks =new KSuser();

HSSFRow[] rows = null;
if(list != null && list.size()>0)
rows = new HSSFRow[list.size()];

int j = 1;
for(int k=0;k <list.size();k++)
ks =list.get(k);
//设置行从第二行开始
rows[j-1] =s.createRow((short)(j));
String[] str =new String[7];
str[0]=ks.getKeshi();
str[1]=ks.getBanzu();
str[2]=ks.getReg_name();
str[3]=ks.getSubject();
str[4]=ks.getText();
str[5]=ks.getRe_day().toString();
str[6]=ks.getStatus();
for(int i=1;i <8;i++)
HSSFCell cell =rows[j-1].createCell((short)(i-1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(str[i-1]);

j++;


wb.write(fos);
fos.close();
catch(Exception e)
e.printStackTrace();
out.println(" <script>alert('异常操作,文件可能正保护中\\n请重试!');history.go(-1); </script>");

String urlname = request.getRequestURI();
urlname = urlname.replaceAll("excel", "excelexport");
urlname = urlname+"/"+filename;
System.out.println("urlname:"+urlname);
//request.getSession().setAttribute("excelfile", urlname);
response.sendRedirect(urlname);
参考技术B 可以使用Apache下的poi工具包进行导出。
具体示例代码可以上Apache官网上找下,或者百度下有很多的。
参考技术C 使用POI吧

如何把网络的网页表格数据导入到Excel表中

将网络上面的网页表格数据导入Excel表格的方法如下(以Excel 2010为例):

    打开Excel表格,点击【数据】,【自网站】;

    在地址栏上面输入来自网站数据的网址链接(可以直接复制浏览器上面的网页地址后进行粘贴),之后按下回车键或点击【转到】;

    待网页上面的数据显示出来后,点击【导入】;

    选择导入的位置并点击【确定】;

    导入完成。

参考技术A

“把网页表格数据导入到Excel表中”的操作步骤是:

1、以Excel 2016为例,打开Excel工作表;

2、在“数据”选项下的“获取外部数据”中,选择“自网站”,然后在“新建Web查询”的地址栏,输入网页数据所在的网址链接,按“转到”;

3、单击“导入”数据左上角的“导入”按钮;

4、将数据纳入“导入”对象;

5、按“导入”,在弹出的“导入数据”编辑框中,点击“属性”;

6、在“外部数据区域属性”对话框中,勾选“允许后台刷新”及“刷新频率”,确定;

7、再点“确定”后,网页数据开始导入Excel工作表,且每隔间隔时间更新一遍。

参考技术B 打开excel文件后,点击数据---自网站--输入网址 选择需要的网页数据,完成后点击“导入‘ 问题解决

以上是关于如何将jsp 中的数据导入到excel表格 中的主要内容,如果未能解决你的问题,请参考以下文章

将多个网页中的数据导入到一个Excel表中 在线等·

VBA实现将EXCEL数据导入WORD表格

如何将一个网页中自己想要的数据导入到Excel表格中

如何把网络的网页表格数据导入到Excel表中

ASP.NET中如何将Excel表中的数据导入到数据库中?

如何将excel表格中的数据转换到arcgis中