java poi操作创建xslx或xsl文件,存本地和进行网络传输两种方式集成

时间:2022-08-13 19:15:00
 package com.java.zxf.util;

 import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* 用来生成xsls或xsl文件的类,生成后可以进行传输下载
* 注意:前台请不要使用ajax方式发送下载请求,因为其不支持.
* @author Administrator
*
*/
public class XSLXUtil {
/**
*
* @param response
* @param titlelist:一个list集合,用来指定表头信息
* @param objlist:一个list结合嵌套,内存list就是表头字段对应得一行数据
* @param filename:一个文件名,可以自己指定
* @throws IOException
*/
public static void getbook(HttpServletResponse response,List<String> titlelist,List<List<String>> objlist,String filename) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();
//建立新的sheet对象(excel的表单)
HSSFSheet sheet=wb.createSheet();
//这里是指定每列的宽度256=1字符,依次类推
for(int x=0;x<ShowString.GDTITLELEN.length;x++){
sheet.setColumnWidth(x,ShowString.GDTITLELEN[x]);
}
//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
//HSSFRow row1=sheet.createRow(0);
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
//HSSFCell cell=row1.createCell(0);
//设置单元格内容
//cell.setCellValue("学员考试成绩一览表");
//合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
//sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
//在sheet里创建第二行 HSSFRow row1=sheet.createRow(0);
for(int i=0;i<titlelist.size();i++){
row1.createCell(i).setCellValue(titlelist.get(i));
}
for(int j=0;j<objlist.size();j++){
HSSFRow row=sheet.createRow(j+1);
List<String> temp = (List<String>) objlist.get(j);
for(int k=0;k<temp.size();k++){
HSSFCell cell = row.createCell(k);
cell.setCellValue(temp.get(k));
}
}
//测试阶段,将文件输出到本地进行存储
/*OutputStream os = new FileOutputStream("c:\\"+filename);
wb.write(os);
os.flush();
os.close();
SystemOut.classSystemOut("xslx文件创建成果", "");*/
response.reset();
OutputStream output=response.getOutputStream();
response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/msexcel");
wb.write(output);
output.close();
SystemOut.classSystemOut("xslx文件创建成果", "");
}
}
 package com.java.zxf.util;

 import java.io.File;

 public interface ShowString {

     public static String picstorage = "imgs"+File.separator+"upload";

     public static String picstorage1 = "imgs/upload";

     public static int defaultEvaluatetime = 7;

     public static int expireinterval = 2;

     public static String GDTYPE = "gd";

     public static String TJTYPE = "tj";

     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};

     public static String GDTITLE[] = {"工单编号","机器编号","机器类型","报修人","公司名称","报修手机号","故障归类","工单发起时间","工单结束时间","故障现象","故障描述","报修地址","机器型号","期待服务时间","确认故障","过期时间","是否服务端报修","工单第一提报人","机器其它参数"};

     public static String TJTITLE[] = {};

 }