java poi操作创建xslx或xsl文件,存本地和进行网络传输两种方式集成
1 package com.java.zxf.util;2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.net.URLEncoder;
6 import java.util.List;
7 import javax.servlet.http.HttpServletResponse;
8 import org.apache.poi.hssf.usermodel.HSSFCell;
9 import org.apache.poi.hssf.usermodel.HSSFRow;
10 import org.apache.poi.hssf.usermodel.HSSFSheet;
11 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
12 /**
13 * 用来生成xsls或xsl文件的类,生成后可以进行传输下载
14 * 注意:前台请不要使用ajax方式发送下载请求,因为其不支持.
15 * @author Administrator
16 *
17 */
18 public class XSLXUtil {
19 /**
20 *
21 * @param response
22 * @param titlelist:一个list集合,用来指定表头信息
23 * @param objlist:一个list结合嵌套,内存list就是表头字段对应得一行数据
24 * @param filename:一个文件名,可以自己指定
25 * @throws IOException
26 */
27 public static void getbook(HttpServletResponse response,List<String> titlelist,List<List<String>> objlist,String filename) throws IOException{
28 HSSFWorkbook wb = new HSSFWorkbook();
29 //建立新的sheet对象(excel的表单)
30 HSSFSheet sheet=wb.createSheet();
31 //这里是指定每列的宽度256=1字符,依次类推
32 for(int x=0;x<ShowString.GDTITLELEN.length;x++){
33 sheet.setColumnWidth(x,ShowString.GDTITLELEN[x]);
34 }
35 //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
36 //HSSFRow row1=sheet.createRow(0);
37 //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
38 //HSSFCell cell=row1.createCell(0);
39 //设置单元格内容
40 //cell.setCellValue("学员考试成绩一览表");
41 //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
42 //sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
43 //在sheet里创建第二行
44
45 HSSFRow row1=sheet.createRow(0);
46 for(int i=0;i<titlelist.size();i++){
47 row1.createCell(i).setCellValue(titlelist.get(i));
48 }
49 for(int j=0;j<objlist.size();j++){
50 HSSFRow row=sheet.createRow(j+1);
51 List<String> temp = (List<String>) objlist.get(j);
52 for(int k=0;k<temp.size();k++){
53 HSSFCell cell = row.createCell(k);
54 cell.setCellValue(temp.get(k));
55 }
56 }
57 //测试阶段,将文件输出到本地进行存储
58 /*OutputStream os = new FileOutputStream("c:\\"+filename);
59 wb.write(os);
60 os.flush();
61 os.close();
62 SystemOut.classSystemOut("xslx文件创建成果", "");*/
63 response.reset();
64 OutputStream output=response.getOutputStream();
65 response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode(filename, "UTF-8"));
66 response.setContentType("application/msexcel");
67 wb.write(output);
68 output.close();
69 SystemOut.classSystemOut("xslx文件创建成果", "");
70 }
71 }
1 package com.java.zxf.util;2
3 import java.io.File;
4
5 public interface ShowString {
6
7 public static String picstorage = "imgs"+File.separator+"upload";
8
9 public static String picstorage1 = "imgs/upload";
10
11 public static int defaultEvaluatetime = 7;
12
13 public static int expireinterval = 2;
14
15 public static String GDTYPE = "gd";
16
17 public static String TJTYPE = "tj";
18
19 public static int[] GDTITLELEN = {40*256,256*20,256*20,12*256,30*256,12*256,20*256,15*256,15*256,20*256,30*256,50*256,16*256,15*256,20*256,15*256,10*256,15*256,150*256};
20
21 public static String GDTITLE[] = {"工单编号","机器编号","机器类型","报修人","公司名称","报修手机号","故障归类","工单发起时间","工单结束时间","故障现象","故障描述","报修地址","机器型号","期待服务时间","确认故障","过期时间","是否服务端报修","工单第一提报人","机器其它参数"};
22
23 public static String TJTITLE[] = {};
24
25 }
以上是 java poi操作创建xslx或xsl文件,存本地和进行网络传输两种方式集成 的全部内容, 来源链接: utcz.com/z/391816.html