導入包這一些不多說,直接貼出關鍵代碼,JSP只要點一個Action鏈接就行。
poi包我是用:poi-3.11-20141221.jar
親測有效:
效果:
Action 類代碼:
private InputStream inputStream; //(get,set方法省略)定義一個輸入流,用於接住在Service類生成的含有EXCEL的輸入流
public String exportNetworkDeviceList() throws Exception {
setInputStream(networkDeviceService.exportNetworkDeviceList(NET_STATUS, NET_MODEL_NUMBER, NET_BUILDING, NET_FLOOR, NET_LOCATION));
return "getNetworkDeviceExportList";
}
Service類代碼:(生成EXCEL表格代碼在Service類寫)
public InputStream exportNetworkDeviceList(String netStatus,
String netModelNumber, String netBuilding, String netFloor,
String netLocation) {
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("表一");
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,創建單元格,並設置值表頭 設置表頭居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 創建一個居中格式
//寫列名,視自己的需求而定
HSSFCell cell = row.createCell(0);
cell.setCellValue("設備型號");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("端口數");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("設備名稱");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("狀態");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("樓宇");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("樓層");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("位置");
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("接口");
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("IP地址");
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("網關");
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("備注");
cell.setCellStyle(style);
//構造數據庫查詢語句,待會我用與從DAO類取數據,視自己的需求而定,個人建議將這一部分寫在另一個方法里面
String hql = "from NetworkDevice ";
if (netStatus == null) {
} else {
if (netStatus.equalsIgnoreCase("00")) {
hql += "n where n.NET_STATUS!=null ";
} else {
hql += "n where n.NET_STATUS='" + netStatus + "' ";
}
;
if (!netModelNumber.isEmpty()) {
hql += "AND n.NET_MODEL_NUMBER = '" + netModelNumber + "' ";
}
;
if (!netBuilding.isEmpty()) {
hql += "AND n.NET_BUILDING = '" + netBuilding + "' ";
}
;
if (!netFloor.isEmpty()) {
hql += "AND n.NET_FLOOR = '" + netFloor + "' ";
}
;
if (!netLocation.isEmpty()) {
hql += "AND n.NET_LOCATION = '" + netLocation + "' ";
}
;
}
hql += "order by 1 DESC";
// 第五步,寫入實體數據 實際應用中這些數據從數據庫得到,
List<NetworkDevice> exportList = networkDeviceDaoImpl.exportNetworkDeviceList(hql);
for (int i = 0; i < exportList.size(); i++) {
row = sheet.createRow((int) i + 1);
NetworkDevice netDevice = exportList.get(i);
// 第四步,創建單元格,並設置值
row.createCell(0).setCellValue(netDevice.getNET_MODEL_NUMBER());
row.createCell(1).setCellValue(netDevice.getNET_DEVICE_PORT());
row.createCell(2).setCellValue(netDevice.getNET_DEVICE_NAME());
row.createCell(3).setCellValue(netDevice.getNET_STATUS());
row.createCell(4).setCellValue(netDevice.getNET_BUILDING());
row.createCell(5).setCellValue(netDevice.getNET_FLOOR());
row.createCell(6).setCellValue(netDevice.getNET_LOCATION());
row.createCell(7).setCellValue(netDevice.getNET_INTERFACE());
row.createCell(8).setCellValue(netDevice.getNET_IP());
row.createCell(9).setCellValue(netDevice.getNET_GATEWAY());
row.createCell(10).setCellValue(netDevice.getNET_REMARK());
}
//自動設置EXCEL的列寬,視自己的需求而定,也可以用sheet.setDefaultColumnWidth(13);為全部列的列寬設置默認值
sheet.autoSizeColumn((short)0);
sheet.autoSizeColumn((short)2);
sheet.autoSizeColumn((short)6);
sheet.autoSizeColumn((short)7);
sheet.autoSizeColumn((short)8);
sheet.autoSizeColumn((short)9);
sheet.autoSizeColumn((short)10);
//設置文件名,用格式化日期來生成一個ID
String filePath="";
Date dt = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String date = df.format(dt).toString();
filePath = "NetDevice" + date + ".xls";
File file=new File(filePath);
try{
OutputStream out=new FileOutputStream(file);
wb.write(out);
out.close();
}catch(Exception e){
e.printStackTrace();
}
InputStream in=null;
try{
in=new FileInputStream(file);
}catch(Exception e)
{
e.printStackTrace();
}
return in;
}
strust2代碼:
<action name="ExportNetworkDeviceList" class="com.javaweb.action.NetworkDeviceAction"
method="exportNetworkDeviceList">
<result name="getNetworkDeviceExportList" type="stream">
<param name="inputStream">excelStream</param>
<param name="ContentType">application/vnd.ms-excel</param>
<param name="contentDisposition">filename="NetDevice.xls"</param>
</result>
</action>
PS:據網友@puyans反饋,若strust2的代碼寫:<param name="inputStream">excelStream</param> 控制台會報:
Cannot create type class java.io.InputStream from value excelStream - [unknown location]
若出現這種情況,請改為:
<param name="inputStream">inputStream</param>