- Java代码
- packagecom.ljz;
- importjava.io.BufferedInputStream;
- importjava.io.BufferedOutputStream;
- importjava.io.ByteArrayInputStream;
- importjava.io.ByteArrayOutputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjavax.servlet.ServletException;
- importjavax.servlet.ServletOutputStream;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importorg.apache.poi.hssf.usermodel.HSSFCell;
- importorg.apache.poi.hssf.usermodel.HSSFRow;
- importorg.apache.poi.hssf.usermodel.HSSFSheet;
- importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
- publicclassdownloadExcelServletextendsHttpServlet{
- privatestaticfinallongserialVersionUID=1L;
- publicdownloadExcelServlet(){
- super();
- }
- publicvoiddestroy(){
- super.destroy();
- }
- privateInputStreamgetInputStream(){
- HSSFWorkbookwb=newHSSFWorkbook();
- HSSFSheetsheet=wb.createSheet("sheet1");
- HSSFRowrow=sheet.createRow(0);
- HSSFCellcell=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");
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ByteArrayOutputStreamos=newByteArrayOutputStream();
- try{
- wb.write(os);
- }catch(IOExceptione){
- e.printStackTrace();
- }
- byte[]content=os.toByteArray();
- InputStreamis=newByteArrayInputStream(content);
- returnis;
- }
- publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
- throwsServletException,IOException{
- response.reset();
- response.setContentType("application/vnd.ms-excel;charset=utf-8");
- response.setHeader("Content-Disposition","attachment;filename="
- +newString("系统日志.xls".getBytes(),"iso-8859-1"));
- ServletOutputStreamout=response.getOutputStream();
- BufferedInputStreambis=null;
- BufferedOutputStreambos=null;
- try{
- bis=newBufferedInputStream(new FileInputStream(file);
- bos=newBufferedOutputStream(out);
- byte[]buff=newbyte[2048];
- intbytesRead;
-
- while(-1!=(bytesRead=bis.read(buff,0,buff.length))){
- bos.write(buff,0,bytesRead);
- }
- }catch(finalIOExceptione){
- System.out.println("IOException.");
- throwe;
- }finally{
- if(bis!=null)
- bis.close();
- if(bos!=null)
- bos.close();
- }
- }
- publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
- throwsServletException,IOException{
- }
- publicvoidinit()throwsServletException{
-
- }
- }
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;
}