POI匯出Excel檔案

2021-07-10 05:51:30 字數 2387 閱讀 7145

workbook workbook = exportservice.exportfile(request, student);

outputstream out = null;

try catch (ioexception e) finally catch (exception e) {}

}

實現類:

// 設定各列寬度

short studentwidths = new short ;

mapproperties = getpropertiesstudent(request, student);

//設定按學號公升序排列取出資料

linkedhashmaporderby = new linkedhashmap();

orderby.put("id", "asc");

//取出所有滿足條件的資料

liststudents = studentdao.getstudentbymap(properties, orderby);

if (students != null && students .size() > 0) else if ("姓名".equals(property)) else if ("年齡".equals(property)) else if ("性別".equals(property))

}//增加新的一行

excelutil.createcommonrow(commrowvalues, count++);

}

}// 調整列寬

for (int i = 0; i < studentwidths.length; i++)

return workbook;

}

將request裡的請求資料封裝到properties形式的map中:

獲取student資料:

public list getstudentsbymap(mapproperties,linkedhashmaporderby)

構造查詢語句:

public string getquerystrstudent(mapproperties, listobjs) else 

} } else

}// 姓名

if (properties.containskey("name")) else

} }}

// 年齡

if (properties.containskey("age")) catch (exception e)

objs.add(age);

} else

}// 性別

if (properties.containskey("***")) else }}

return builder.tostring();

}

工具類://設定excel**屬性

public exportexcelutil(workbook workbook, sheet sheet)

/*** 設定排序條件(order by *** desc,yyy asc,zzz desc)

* * @param orderby

*            排序linkedhashmap

* @return string 排序語句

*/

public static string buildorderby(linkedhashmaporderby) 

// 刪除多餘的乙個逗號

orderbysql.deletecharat(orderbysql.length() - 1);

}return orderbysql.tostring();

}

使用POI匯出excel檔案

fileoutputstream fileout new fileoutputstream workbook wb new hssfworkbook 建議使用介面來宣告變數 面向介面思想,可以方便使用介面的不同實現 wb.write fileout 寫入檔案 fileout.close sheet ...

使用poi匯出excel檔案

1.匯入依賴 org.apache.poigroupid poiartifactid 4.1.2version dependency 2.獲取匯出資料 list data 資料 3.設定匯出元件 hssfworkbook workbook newhssfworkbook hssfsheet shee...

利用poi匯出excel

最近接了乙個任務,要求把資料庫內容提取成excel,乙個excel動態建立多個sheet,乙個sheet裡面顯示一條資訊,然後再根據此條資訊的結果,再去子表獲取結果,如一條學生資訊,對應顯示該學生的所有詳細資訊。如圖所示 工程中匯入 public static void writeexcelnew ...