1. MIME类型的设置:
Web 浏览器使用 MIME 类型来识别非 HTML 文档,并决定如何显示该文档内的数据。
例如EXCEL文件的 MIME 类型是 "application/vnd.ms-excel "。要用servlet 来打开一个 EXCEL 文档,需要将 response 对象中 header 的 contentType 设置成“application/vnd.ms-excel ”。
response.setContentType(contentType);
2. Content disposition
HTTP response header中的content-disposition 允许 servlet 指定文档表示的信息。使用这种header ,你就可以将文档指定成单独打开(而不是在浏览器中打开),还可以根据用户的操作来显示。
如果用户要保存文档,你还可以为该文档建议一个文件名。这个建议名称会出现在 Save As 对话框的“文件名”栏中。如果没有指定,则对话框中就会出现 servlet 的名字。
servlet 中,将 header 设置成下面这样:
response.setHeader("Content-disposition","p_w_upload;filename="+ "Example.xls" );
response.setHeader("Content-Disposition", "inline; filename="fliename)
点击打开会在ie中打开。
需要说明的有三点:
Ø 中文文件名需要进行iso8859-1转码方可正确显示:
fileName = new String(fileName.getBytes("GBK"),"iso8859-1");
Ø 传递的文件名,需要包含后缀名(如果此文件有后缀名),否则丢失文件的属性,而不能自行选择相关程序打开。
Ø 有下载前询问(是打开文件还是保存到计算机)和通过IE浏览器直接选择相关应用程序插件打开两种方式,前者如上代码所示,后者如下:
response.setHeader("Content-disposition","filename="+ "Example.xls" );
3. 在研究文件的上传及下载过程中,有几点体会
程序的I/O操作往往是性能的瓶颈所在,java io定义了两个基本的抽象类:InputStream和OutputStream,对于不同的数据类型比如磁盘,网络又提供了不同的实现,java.io也提供了一些缓冲流(BufferedStream),使硬盘可以很快的读写一大块的数据, 而Java基本的I/O类一次只能读写一个字节,但缓冲流(BufferedStream)可以一次读写一批数据,,缓冲流(Buffered Stream)大大提高了I/O的性能。所以:
Ø小块小块的读写数据会非常慢,因此,尽量大块的读写数据
Ø使用BufferedInputStream和BufferedOutputStream来批处理数据以提高性能
Ø对象的序列化(serialization)非常影响I/O的性能,尽量少用
<%
String sorefitem=request.getParameter("SOREFITEM");
sorefitem = "24326";
String sheetname = "productsitems";
String excelsql = "select oi.sorefitem,po.psuppliercompany suppliername,oi.pname productname,oi.ivendpartnumber productcode,oi.oqty quantity,oi.osellprice price,oi.pricecurrefitem currency,po.ponumber "
+" from ecorders.orderitems oi,ecorders.purchaseorders po "
+" where (oi.ostatus>3 or oi.ostatus=3 and oi.osubstatus=4) AND oi.sorefitem="+sorefitem
+" AND oi.sorefitem=po.sorefitem and oi.osellerid=po.psupplierid" ;
int col_num = 0;
java.sql.Statement stm = null;
java.sql.ResultSet rs = null;
java.sql.Connection conn = null;try...{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch (ClassNotFoundException e)
...{ System.out.println ("no exist driver");
System.exit(0);
}
try
...{
response.reset();
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition" ,"p_w_upload;filename="+new String("导出采购商明细报表Excel.xls".getBytes(),"iso-8859-1"));
OutputStream os = response.getOutputStream();//将 WritableWorkbook 写入到输出流
jxl.write.WritableWorkbook wwb = jxl.Workbook.createWorkbook(os); //创建Excel工作表
jxl.write.WritableSheet ws = wwb.createSheet("sheet_1",0);
col_num = 7;
jxl.write.Label label_head;
label_head = new jxl.write.Label(0,0,"请购单号");
ws.addCell(label_head);
label_head = new jxl.write.Label(1,0,"供应商名");
ws.addCell(label_head);
label_head = new jxl.write.Label(2,0,"产品规格型号");
ws.addCell(label_head);
label_head = new jxl.write.Label(3,0,"产品编码");
ws.addCell(label_head);
label_head = new jxl.write.Label(4,0,"数量");
ws.addCell(label_head);
label_head = new jxl.write.Label(5,0,"单价");
ws.addCell(label_head);
label_head = new jxl.write.Label(6,0,"定单号");
ws.addCell(label_head);//结果集中的数据添加到excel中
jxl.write.Number label_num;
jxl.write.Label label_str;
jxl.write.WritableCellFormat floatFormat;
int numrow = 1;
System.out.println(System.currentTimeMillis()+" welcome to hehe!!!!!!!!!!!");
conn = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.180:1521:DB","system","manager");
stm = conn.createStatement();
rs = stm.executeQuery(excelsql);
while(rs.next())
...{
System.out.println(System.currentTimeMillis()+" welcome to while!!!!!!!!!");
label_num = new jxl.write.Number(0,numrow,rs.getInt("sorefitem"));
ws.addCell(label_num);
label_str = new jxl.write.Label(1,numrow,rs.getString("suppliername"));
ws.addCell(label_str);
label_str = new jxl.write.Label(2,numrow,rs.getString("productname"));
ws.addCell(label_str);
label_str = new jxl.write.Label(3,numrow,rs.getString("productcode"));
ws.addCell(label_str);
floatFormat = new jxl.write.WritableCellFormat (jxl.write.NumberFormats.FLOAT);
label_num = new jxl.write.Number(4,numrow,rs.getDouble("quantity"),floatFormat);
ws.addCell(label_num);
label_num = new jxl.write.Number(5,numrow,rs.getDouble("price"),floatFormat);
ws.addCell(label_num);
label_str = new jxl.write.Label(6,numrow,rs.getString("ponumber"));
ws.addCell(label_str);
numrow ++;
}
wwb.write();
wwb.close();
os.flush();
os.close();
}
catch(java.sql.SQLException sqle)...{
System.out.println("sql error");
}
catch(Exception e)...{
System.out.println("catch error ------");
e.printStackTrace();
}
finally...{
System.out.println(System.currentTimeMillis()+ "welcome to finally!!!!!!!");
if(stm != null)
try...{
stm.close();
}catch(Exception fe) ...{ }
if(conn != null)
try...{
conn.close();
}
catch(Exception fe2)...{ }
}
%>
JXL包下载页面:http://sourceforge.net/project/showfiles.php?group_id=79926
如果要输出文件到服务器保存:
jxl.write.WritableWorkbook wwb = jxl.Workbook.createWorkbook(new File("D:/JSPweb/112WebModule1/download_excel/"+excelfilename+"交接箱统计故障数.xls")); //创建Excel工作表