org.apache.poi中的jar包准备
- poi-ooxml 3.14
- poi-ooxml-schemas 3.14(可以不用)
- poi 3.14
package tian;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import com.graphbuilder.struc.LinkedList;
//业务处理方法,更具业务需求自行封装
public class DownLoadExcel {
public void downloadEecel() {
SXSSFWorkbook sxssfworkbook=null;
SXSSFRow nRow=null;
Cell nCell=null;
FileOutputStream fileO=null;
String path=this.getClass().getClassLoader().getResource("").getPath();
String fileName="tem.xlsx" ;
File file=new File(path+fileName);
if(file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
List list=(List) new LinkedList();
String []strList= {};
sxssfworkbook=new SXSSFWorkbook(100);
CellStyle cellSstyle = sxssfworkbook.createCellStyle();
cellSstyle.setAlignment(CellStyle.ALIGN_CENTER);// 设置居中
for(int j=0;j<list.size();j++) {
nRow=sxssfworkbook.createSheet().createRow(1);
for(int i=0;i<strList.length;i++) {
nCell.setCellValue("值");
}
}
}
// 还回给浏览器数据流用于下载
public static void download(String fileName, String filePath, HttpServletResponse response) {
File tempFile = new File(filePath + fileName);
InputStream is = null;
ServletOutputStream out=null;
response.addHeader("Content-Disposition", "attachment:filename=" + fileName);
response.addHeader("Connection", "close");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
Cookie cook = new Cookie("filename", "true");
cook.setPath("/");
try {
is = new FileInputStream(filePath + fileName);
out = response.getOutputStream();
byte[] content = new byte[1024];
int length = 0;
while ((length = is.read(content)) != -1) {// 请留意read里面的参数
out.write(content, 0, length); // 将数据流给web
}
response.reset();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 必要地清除response中的缓存信息response.setContType("applaication/vnd.openxmlformats-
// officedocument.spreadsheetml.sheet;charset=utf-8")
// java.net.URLEncoder.encode(fileName, "UTF-8"));
}
}
关于样式问题:样式创建:CellStyle cellStyle=(new SXSSFWorkBook()).createCellStyle();
cellStyle.setAlignment(CellStyle ."要加的水平方式");
边框:cellStyle.setBorder"边框位置"(CellStyle ."选择");
颜色:cellStyle.setBottonBorderColor(HSSFColor.“要选择的颜色”);自定义颜色用:Palatte。
最后一步:将设定好的样式放入到创建的单元格中:如:((new Row()).creatCell(1)).setCellStyle(cellStyle)
(主要还是流的应用,要去了解输入输出流才行,具体代码参见其它只提供思路和包路径,还要,要相信自己,
先写出下载再做更改很