Java下载文件以及DOM输出为EXCEL

public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
try
{

String filename=domtoExcel(dom,hs);
//response.setContentType("application/vnd.ms-excel"); //该头直接在IE中打开Excel文件

response.setContentType("application/octet-stream;charset=GB2312");
String downloadname="Detail.xls";
response.setHeader("Content-Disposition","attachment; filename="+downloadname);

if (filename!=null)
{
File file1=new File(filename);
FileInputStream is=new FileInputStream(file1);
byte []bbb=new byte[(int)file1.length()];

System.out.println("------bbb.length:"+bbb.length);
is.read(bbb);
is.close();
OutputStream out = response.getOutputStream();
out.write(bbb);
out.close();
response.setStatus(response.SC_OK);
file1.delete();
}
else
{
System.out.println("----createExcelFile error!");
}
}
catch (Exception e)
{
System.out.println("----------ex in servlet:"+e);
e.printStackTrace();
}
}


public String domtoExcel(Document dom,Hashtable hs)
{
String title=(String)hs.get("title");

String filename=getFilename();
if (filename==null) return null;
filename="data/"+filename;
boolean isOK=false;
try
{
if(dom==null)
{
System.out.println("dom is null");
}
dom.normalize();
Node root=(Node)dom.getDocumentElement();
NodeList nodeRowList=root.getChildNodes();

WritableWorkbook workbook=Workbook.createWorkbook(new File(filename));
/************创建工作表*************/

WritableSheet sheet=workbook.createSheet(title,0);
//setHeaderFooter(sheet2,hs);
setHeaderFooter(sheet,hs);
WritableFont normalFont= new WritableFont(WritableFont.ARIAL,10);
WritableCellFormat wcf_left=new WritableCellFormat(normalFont);
wcf_left.setBorder(Border.ALL,BorderLineStyle.THIN); //线条
wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直对齐
wcf_left.setAlignment(Alignment.LEFT);
// wcf_left.setWrap(false); //是否换行

//添加行宽设置
Map mapWidth=(Map)hs.get("widthMap");
if(mapWidth!=null&&mapWidth.size()>0){
Iterator it = mapWidth.keySet().iterator();
while(it.hasNext()){
String str = (String)it.next();
sheet.setColumnView(Integer.parseInt(str), Integer.parseInt((String)mapWidth.get(str)));
}
}

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);
sheet.addCell(new Label(j,0,nodeItem.getChildNodes().item(0).getChildNodes().item(0).getNodeValue(),
wcf_left));
}
}

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);
String itemtype=nodeItem.getChildNodes().item(2).getChildNodes().item(0).getNodeValue(); //itemtype
itemtype=itemtype.trim();
NodeList temp=nodeItem.getChildNodes().item(1).getChildNodes();
String str1 = "";
if(nodeItem.getChildNodes().item(1).getChildNodes().item(0)==null)
{
str1="";
}
else
{
str1=nodeItem.getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
}

sheet.addCell(new Label(j,i+1,str1==null?"":str1,wcf_left));



}
}
String footerLeft=(String)hs.get("footerLeft");
String footerCenter=(String)hs.get("footerCenter");
String footerRight=(String)hs.get("footerRight");

int row=nodeRowList.getLength()+1;
WritableCellFormat wcf_none = new WritableCellFormat();
wcf_none.setBorder(Border.NONE, BorderLineStyle.THIN);
sheet.addCell(new Label(0,row,footerLeft==null ? "":footerLeft,wcf_none));
sheet.addCell(new Label(2,row,footerCenter==null ? "":footerCenter,wcf_none));
sheet.addCell(new Label(4,row,footerRight==null ? "":footerRight,wcf_none));

workbook.write();
workbook.close();
isOK=true;
System.out.println("--------dom export ok-------");
}
catch (Exception e)
{
System.out.println("------exception in dom2Csv:"+e);
e.printStackTrace();
}

if (isOK)
{
return filename;
}
else
{
return null;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值