-
Java代码
-
package com.ljz;
-
import java.io.BufferedInputStream;
-
import java.io.BufferedOutputStream;
-
import java.io.ByteArrayInputStream;
-
import java.io.ByteArrayOutputStream;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import javax.servlet.ServletException;
-
import javax.servlet.ServletOutputStream;
-
import javax.servlet.http.HttpServlet;
-
import javax.servlet.http.HttpServletRequest;
-
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;
-
public class downloadExcelServletextends HttpServlet {
-
private static final long serialVersionUID = 1L;
-
public downloadExcelServlet() {
-
super();
-
}
-
public void destroy() {
-
super.destroy();
-
}
-
private InputStream getInputStream() {
-
HSSFWorkbook wb = new HSSFWorkbook();
-
HSSFSheet sheet = wb.createSheet("sheet1");
-
HSSFRow row = sheet.createRow(0);
-
HSSFCell cell = row.createCell((short) 0);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue("序号");
-
cell = row.createCell((short) 1);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue("姓");
-
cell = row.createCell((short) 2);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue("名");
-
cell = row.createCell((short) 3);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue("年龄");
-
-
row = sheet.createRow(1);
-
cell = row.createCell((short) 0);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue("1");
-
cell = row.createCell((short) 1);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue("刘");
-
cell = row.createCell((short) 2);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue("继忠");
-
cell = row.createCell((short) 3);
-
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-
cell.setCellValue("25");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
ByteArrayOutputStream os = new ByteArrayOutputStream();
-
try {
-
wb.write(os);
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
byte[] content = os.toByteArray();
-
InputStream is = new ByteArrayInputStream(content);
-
return is;
-
}
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
response.reset();
-
response.setContentType("application/vnd.ms-excel;charset=utf-8");
-
response.setHeader("Content-Disposition", "attachment;filename="
-
+ new String("系统日志.xls".getBytes(),"iso-8859-1"));
-
ServletOutputStream out = response.getOutputStream();
-
BufferedInputStream bis = null;
-
BufferedOutputStream bos = null;
-
try {
-
bis = new BufferedInputStream(new
FileInputStream(file);
-
bos = new BufferedOutputStream(out);
-
byte[] buff = new byte[2048];
-
int bytesRead;
-
-
while (-1 != (bytesRead = bis.read(buff,0, buff.length))) {
-
bos.write(buff, 0, bytesRead);
-
}
-
} catch (final IOException e) {
-
System.out.println("IOException.");
-
throw e;
-
} finally {
-
if (bis != null)
-
bis.close();
-
if (bos != null)
-
bos.close();
-
}
-
}
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
}
-
public void init() throws ServletException {
-
-
}
-
}
public class UploadAction{
private TPoiService ts;
public TPoiService getTs() {
return ts;
}
public void setTs(TPoiService ts) {
this.ts = ts;
}
public File getNewFile(String path){
File file=new File(path);
File[] dir=file.listFiles();
System.out.println(dir.length);
int[] newfiles=new int[dir.length];
for (int i = 0; i < dir.length; i++) {
// System.out.println(dir[i]);
newfiles[i]=Integer.parseInt(dir[i].getName().substring(0,dir[i].getName().indexOf(".")));
}
int num1=newfiles[0];
for (int j = 0; j < newfiles.length; j++) {
if(newfiles[j]>num1){
num1=newfiles[j];
}
}
System.out.println(num1);
return new File(path+"\\"+num1+".xls");
}
@Action(value="upload",className="upload",results={@Result(name="success",location="/upload.jsp",type="dispatcher"),
@Result(name="error",location="/Page/error.jsp",type="dispatcher")})
public String upload() throws IOException{
System.out.println("this is upload");
HttpServletResponse response=ServletActionContext.getResponse();
// PrintWriter out = response.getWriter();
File file=getNewFile(ServletActionContext.getRequest().getRealPath("xls"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String("isd.xls".getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(file) {
@Override
public int read() throws IOException {
// TODO Auto-generated method stub
return 0;
}
});
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
System.out.println("IOException.");
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
return null;
}