第一步:先重資料庫查詢出所需要的資料字段內容
第二步:上面那個方法呼叫了excel匯出工具方法
public int outexcel(string path,string headertitle,list> datalist)else if(path.endswith(".xlsx"))else
// 第二步,在workbook中新增乙個sheet,對應excel檔案中的sheet
sheet sheet = wb.createsheet("sheet1");
// 第三步,在sheet中新增表頭第0行,注意老版本poi對excel的行數列數有限制short
row row = sheet.createrow((int) 0);
// 第四步,建立單元格,並設定值表頭 設定表頭居中
cellstyle style = wb.createcellstyle();
style.setalignment(cellstyle.align_center); // 建立乙個居中格式
style.setfillforegroundcolor(hssfcolor.sea_green.index);
style.setfillpattern(cellstyle.solid_foreground);
style.setborderbottom(cellstyle.border_thin);
style.setborderleft(cellstyle.border_thin);
style.setborderright(cellstyle.border_thin);
style.setbordertop(cellstyle.border_thin);
// 生成乙個字型
font font = wb.createfont();
font.setcolor(hssfcolor.black.index);
font.setfontheightinpoints((short) 12);
font.setboldweight(font.boldweight_bold);
// 把字型應用到當前的樣式
style.setfont(font);
cellstyle style1 = wb.createcellstyle();
style1.setalignment(cellstyle.align_center); // 建立乙個居中格式
style1.setfillforegroundcolor(hssfcolor.black.index);
style1.setfillpattern(cellstyle.solid_foreground);
style1.setborderbottom(cellstyle.border_thin);
style1.setborderleft(cellstyle.border_thin);
style1.setborderright(cellstyle.border_thin);
style1.setbordertop(cellstyle.border_thin);
// 生成乙個字型
font font1 = wb.createfont();
font1.setfontheightinpoints((short) 12);
// 把字型應用到當前的樣式
style1.setfont(font1);
cell cell = null;
int count=0;
string header_title = headertitle.split(",");
for (int i=0;idatamap = datalist.get(i);
for(int j=0;jpublic string filterdangerstring(string value)
value=value.replaceall("<","<");
value=value.replaceall(">",">");
value=value.replaceall("'","\'");
return value;
}
Java 匯出資料到Excel
連線mysql資料庫,查詢出資料,並匯出資料到excel public static void main string args 以filename為檔名來建立乙個workbook wwb workbook.createworkbook file 建立工作表 writablesheet ws wwb...
Java資料匯出到excel
這裡只能從顯示資料的jsp頁面上匯出當前頁面的資料,這些資料可以從資料庫讀出,顯示到jsp頁面,然後加一段話就可把當前頁面資料儲存到excel裡面 如從後台過來資料顯示到 showuser.jsp 然後加鏈結到 showexcel.jsp就可以了,在這個頁面只多了幾行字,其他的都不會顯示包括頁面!但...
java 實現匯出資料到 excel
如下 賬單明細匯出 匯出 第一步,建立乙個webbook,對應乙個excel檔案 hssfworkbook wb new hssfworkbook 第二步,在webbook中新增乙個sheet,對應excel檔案中的sheet hssfsheet sheet wb.createsheet sheet...