Excelエクスポートファイル
// excel ,
public static void writeExcel(ResultSet rs, String fileName,
HttpServletResponse response) throws SQLException, IOException,
RowsExceededException, WriteException {
ResultSetMetaData rsmd = rs.getMetaData();
//
int columnCount = rsmd.getColumnCount();
Vector columnNames = new Vector();
for (int i = 1; i <= columnCount; i++) {
//
columnNames.add(rsmd.getColumnName(i));
}
Vector dataOfRows = new Vector();
dataOfRows.add(columnNames);
while (rs.next()) {
Vector columnValues = new Vector();
for (int i = 1; i <= columnCount; i++) {
try {
String columnValue = rs.getString(i);
if (columnValue == null) {
columnValue = "";
}
columnValues.add(columnValue);
} catch (Exception e) {
//
columnValues.add("");
}
}
dataOfRows.add(columnValues);
}
response.setContentType("application/x-msdownload");
String time = (new Timestamp(System.currentTimeMillis())).toString()
.substring(2, 10).replaceAll("-", "");
//
String dispposition = "=?UTF-8?Q?attachment; filename="
+ Utilities.toUtf8String(fileName + time)
+ ".xls";
response.setHeader("Content-Disposition", dispposition);
OutputStream os = response.getOutputStream();
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
jxl.write.WritableSheet ws = wwb.createSheet(fileName, 0);
for (int i = 0; i < dataOfRows.size(); i++) {
for (int j = 0; j < columnCount; j++) {
Label label = new Label(j, i, ((Vector) dataOfRows.get(i)).get(
j).toString());
ws.addCell(label);
}
}
// Excel
wwb.write();
wwb.close();
os.close();
}
导入って书いただけなのに今回同僚が书いたエクスポートを见て通用するから入れたjxlバッグは绍介しなくてもいいよもう1つ伝えてみんな别のところに探さなくてもいいよ