專案總Excel的匯出總結

2021-06-29 13:05:05 字數 3895 閱讀 3128

1,類中的整體實現直接匯出

response.setcontenttype("octets/stream;charset=utf-8");

string str=urlencoder.encode("產品邊界-銷量匯出.xls","utf-8");

response.setheader("content-disposition","attachment;filename="+str);

outputstream out =response.getoutputstream();

hssfworkbook work=new hssfworkbook();

hssfcellstyle style=work.createcellstyle();

style.setalignment(hssfcellstyle.align_center);

style.setfillbackgroundcolor((short)23);

hssffont font=work.createfont();

font.setcolor(hssffont.color_normal);

style.setfont(font);

hssfsheet sheet=work.createsheet("產品邊界-銷量匯出");

sheet.setdefaultcolumnwidth(25);

//年份的數目

int yearnum=compareform.getyearssize();

excelutil.setcellvalue(sheet, 0, 0, "銷量volume de vente

", style);

sheet.addmergedregion(new region(0,(short)0, 0,  (short)(yearnum+1)));

excelutil.setcellvalue(sheet, 1, 0, "銷量volume de vente

",style);

excelutil.setcellvalue(sheet, 2, 0, compareform.getastage(),style);

excelutil.setcellvalue(sheet, 3, 0, compareform.getbstage(),style);

excelutil.setcellvalue(sheet, 4, 0, compareform.getbstage()+"-"+ compareform.getastage() ,style);

//插入年份

listlist=compareform.getyears1();

mapmap=compareform.getsalemap();

for(int i=0;iexcelutil.setcellvalue(sheet, 1, i+1,list.get(i), style);

excelutil.setcellvalue(sheet, 2, i+1,map.get(list.get(i)).getvalue1() , style);

excelutil.setcellvalue(sheet, 2, i+1, map.get(list.get(i)).getvalue2(), style);

}excelutil.setcellvalue(sheet, 1, yearnum+1, "total", style);

excelutil.setcellvalue(sheet, 2,yearnum+1, compareform.gettotalsum1(), style);

excelutil.setcellvalue(sheet, 3,yearnum+1 , compareform.gettotalsum2(), style);

work.write(out);

out.flush();

out.close();

2,excelutil中的setcellvalue方法

public static void setcellvalue(hssfsheet sheet,int irow, int icolumn, string values,

hssfcellstyle style) throws exception

hssfcell cell = row.getcell((short) icolumn);

if (cell == null)

cell.setcelltype(hssfcell.cell_type_string);

if (values == null)

cell.setcellvalue(values);

cell.setcellstyle(style);

} catch (exception e)

}3,加入模板後的匯出

hssfworkbook work = this.gettemplate(request.getrealpath("/")+"resource/excel/dpmvalue.xls");

hssfsheet sheet=work.getsheetat(0);

hssfcellstyle style=work.createcellstyle();

style.setalignment(hssfcellstyle.align_center);

hssffont font=work.createfont();

font.setcolor(hssffont.color_red);

style.setfont(font);

excelutil.setcellvalue(sheet, 0, 0, "淨現值和moc(%)演變 evolution de van", style);

sheet.addmergedregion(new region(0, (short)0,0, (short)3));

excelutil.setcellvalue(sheet, 1, 1, compareform.getastage(), style);

excelutil.setcellvalue(sheet, 1, 2, compareform.getbstage(), style);

excelutil.setcellvalue(sheet, 2, 1, compareform.getjsumrate()+"", style);

excelutil.setcellvalue(sheet, 2, 2, compareform.getasumrate()+"", style);

//van

if(compareform.getjcalresbasedto()!=null)

if(compareform.getacalresbasedto()!=null)

if(compareform.getgvandto()!=null)

//東風毛利率

if(compareform.getjdfprofitmmdto()!=null)

if(compareform.getadfprofitmmdto()!=null)

if(compareform.getgvandto()!=null)

//經營利潤率moc%

if(compareform.getjcalresbasedto()!=null)

if(compareform.getacalresbasedto()!=null)

if(compareform.getgvandto()!=null)

//投資**期 月duree de recuperation mois

if(compareform.getjcalresbasedto()!=null)

if(compareform.getacalresbasedto()!=null)

}4,獲得模板的方法

public hssfworkbook   gettemplate(string filepath) catch (exception e)

return wb;

}

EXCEL匯出總結

下面是我做乙個excel匯出功能的 前台 function export 要想連線上servlet的方法,需要再web.xml配置檔案中配置一下 excelservlet servlet.excelservlet 按這種寫法,就能順利進入servlet中 然後還有乙個工具類 public class...

java 專案利用POI匯出excel總結

在專案中遇到需要把計畫檢修的專案匯出 使用的是apache poi對資料進行匯出 workbook workbook new hssfworkbook 新建excel sheet sheet workbook.createsheet 維修 檢修計畫詳情 新建工作簿 row row sheet.cre...

gridview 匯出excel 總結

protected void button1 click object sender,eventargs e private void export string filetype,string filename 如果沒有下面方法會報錯型別 gridview 的控制項 gridview1 必須放在具...