如何将jsp 中的数据导入到excel表格 中
将从数据库中查询出来的数据,显示在jsp页面中,想通过点击一个按钮,将显示在jsp页面中的数据导入到excel表格中...
将从数据库中查询出来的数据,显示在jsp 页面中, 想通过点击一个按钮,将显示在jsp 页面中的数据导入到excel 表格中
展开
4个回答
展开全部
可以用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);
//拼凑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);
展开全部
可以用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);
//拼凑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);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以使用Apache下的poi工具包进行导出。
具体示例代码可以上Apache官网上找下,或者百度下有很多的。
具体示例代码可以上Apache官网上找下,或者百度下有很多的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用POI吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |