首页>代码>java servlet页面表格导出Excel(csv格式和xls格式都可以)>/Dom2Table/src/com/zhangjun/edu/exportExcel/Dom2CsvServlet.java
package com.zhangjun.edu.exportExcel;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Date;
import org.w3c.dom.*;
/**
* <p>Description:下载生成CSV格式的EXCEL</p>
* @author 张军
* @version 1.0
* @date 2012-10-02
*/
@SuppressWarnings("serial")
public class Dom2CsvServlet extends HttpServlet
{
static final String CONTENT_TYPE = "text/csv;charset=GB2312";
public void init( ServletConfig config )
throws ServletException
{
super.init( config );
}
public void destroy()
{
}
public String getServletInfo()
{
return (super.getServletInfo());
}
// Handle the HTTP GET request
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
try
{
String sessionName=(String)request.getSession().getAttribute("sname");
Document dom=(Document)request.getSession().getAttribute(sessionName);
if(dom==null || sessionName==null)
{
response.setContentType( CONTENT_TYPE );
PrintWriter out = response.getWriter();
out.println("超时请重新登陆!");
}
else
{
String csv=dom2Csv(dom);
byte []bbb = csv.getBytes("GBK");
response.setContentType("application/octet-stream;charset=GB2312");
String excelName = new Date().getTime()+"";
String filename=excelName+".csv";
response.setHeader("Content-Disposition","attachment; filename="+filename);
OutputStream out2 = response.getOutputStream();
out2.write(bbb);
out2.close();
response.setStatus(response.SC_OK);
}
}
catch (Exception e)
{
System.out.println("----------ex in servlet:"+e);
e.printStackTrace();
}
}
// Handle the HTTP POST request
public void doPost( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
}
private String dom2Csv(Document dom)
{
StringBuffer bf=new StringBuffer();
try
{
dom.normalize();
Node root=(Node)dom.getDocumentElement();
NodeList nodeRowList=root.getChildNodes();
if (nodeRowList.getLength()>0) //dom is not null
{
Node nodeRowTitle=nodeRowList.item(0);
NodeList nodeItemList=nodeRowTitle.getChildNodes();
for (int j=0;j<nodeItemList.getLength();j++)
{
Node nodeItem=nodeItemList.item(j);
if (j!=0)
{
bf.append(",");
}
bf.append("\"");
bf.append(nodeItem.getChildNodes().item(0).getChildNodes().item(0).getNodeValue()); // /documents/row/item/name
bf.append("\"");
}
bf.append("\n");
}
for (int i=0;i<nodeRowList.getLength();i++)
{
Node nodeRow=nodeRowList.item(i);
NodeList nodeItemList=nodeRow.getChildNodes();
for (int j=0;j<nodeItemList.getLength();j++)
{
Node nodeItem=nodeItemList.item(j);
if (j!=0)
{
bf.append(",");
}
String itemtype=nodeItem.getChildNodes().item(2).getChildNodes().item(0).getNodeValue(); //itemtype
itemtype=itemtype.trim();
NodeList temp=nodeItem.getChildNodes().item(1).getChildNodes();
if (temp==null || temp.getLength()==0)
{
bf.append("\"\"");
}
else if (itemtype!=null && (itemtype.equals("CHAR")||itemtype.equals("String")||itemtype.equals("VARCHAR")||itemtype.equals("VARCHAR2")))
{
bf.append("\"");
bf.append(nodeItem.getChildNodes().item(1).getChildNodes().item(0).getNodeValue());
bf.append("\t\"");
}
else
{
bf.append(nodeItem.getChildNodes().item(1).getChildNodes().item(0).getNodeValue());
}
}
bf.append("\n");
}
}
catch (Exception e)
{
e.printStackTrace();
}
return bf.toString();
}
}
最近下载更多
最近浏览更多
cz8857216 LV4
2024年3月8日
fesfefe LV13
2024年1月26日
uni-code_0123 LV1
2023年11月11日
EFWAGGFAWGR
2023年10月19日
暂无贡献等级
微信网友_6467077197238272 LV1
2023年5月8日
lironggang LV38
2023年3月28日
zjc010726
2023年3月15日
暂无贡献等级
qwqw900619 LV4
2022年7月16日
nbzhou2013 LV14
2022年4月4日
一直都会顺利的小吴 LV5
2022年2月16日

