The JAVAWeb SSH framework exports EXCEL using POI, And the pop-up save box appears, javawebpoi
To import a package, simply paste the key code. JSP just needs to click an Action Link.
Poi pack I use: poi-3.11-20141221.jar
Valid for test:
Effect:
Action Code:
Private InputStream inputStream; // (get, set Method omitted) defines an input stream, used to catch the input stream generated in the Service class containing EXCEL
Public String exportNetworkDeviceList () throws Exception {
SetInputStream (networkDeviceService. exportNetworkDeviceList (NET_STATUS, NET_MODEL_NUMBER, NET_BUILDING, NET_FLOOR, NET_LOCATION ));
Return "getNetworkDeviceExportList ";
}
Service Code: (generate the EXCEL table code and write it in the Service class)
Public InputStream exportNetworkDeviceList (String netStatus,
String netModelNumber, String netBuilding, String netFloor,
String netLocation ){
HSSFWorkbook wb = new HSSFWorkbook ();
// Step 2: Add a sheet to the webbook, corresponding to the sheet in the Excel file
HSSFSheet sheet = wb. createSheet ("table 1 ");
// Step 3: Add row 0th in the table header in sheet. Note that the earlier version of poi has a limit on the number of rows and columns in Excel.
HSSFRow row = sheet. createRow (int) 0 );
// Step 4: Create a cell and set the value header to center the header.
HSSFCellStyle style = wb. createCellStyle ();
Style. setAlignment (HSSFCellStyle. ALIGN_CENTER); // create a center format
// Write the column name, depending on your needs
HSSFCell cell = row. createCell (0 );
Cell. setCellValue ("device model ");
Cell. setCellStyle (style );
Cell = row. createCell (1 );
Cell. setCellValue ("port count ");
Cell. setCellStyle (style );
Cell = row. createCell (2 );
Cell. setCellValue ("device name ");
Cell. setCellStyle (style );
Cell = row. createCell (3 );
Cell. setCellValue ("status ");
Cell. setCellStyle (style );
Cell = row. createCell (4 );
Cell. setCellValue ("building ");
Cell. setCellStyle (style );
Cell = row. createCell (5 );
Cell. setCellValue ("floor ");
Cell. setCellStyle (style );
Cell = row. createCell (6 );
Cell. setCellValue ("location ");
Cell. setCellStyle (style );
Cell = row. createCell (7 );
Cell. setCellValue ("interface ");
Cell. setCellStyle (style );
Cell = row. createCell (8 );
Cell. setCellValue ("ip address ");
Cell. setCellStyle (style );
Cell = row. createCell (9 );
Cell. setCellValue ("Gateway ");
Cell. setCellStyle (style );
Cell = row. createCell (10 );
Cell. setCellValue ("Remarks ");
Cell. setCellStyle (style );
// Construct a database query statement. I will use it later and retrieve data from the DAO class, depending on my needs.
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 ";
// Step 5: Write the object data to the database for actual application,
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 );
// Step 4: Create a cell and set the value
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 ());
}
// Automatically set the column width of an EXCEL file based on your needs.
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 );
// Set the file name and generate an ID using the formatted date.
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 code:
<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>