我正在使用struts2.0中的Project工作,我希望通过特定位置的保存对话框保存由java类(使用poi-2.5.1.jar)生成的excel报告。
在我的jsp页面中,我有一个锚标记
<a href="XLSReport">
<img src="images/Excel.gif" style="border: none;"/></a>
在我的操作名称中的XLSReport,它在struts.xml文件中映射
<action name="XLSReport" class="com.gst.petl.report.DesignationListXLSReport" method="execute">
<result name="success" type="redirect">userType.action</result>
</action>
我的Excel报告是通过特定位置的java文件创建的。 我想在点击锚标签后打开这样的对话框。
Java Class是:
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.gst.gspf.core.framework.jdbc.JdbcDAOSupport;
public class DesignationListXLSReport extends JdbcDAOSupport{
HttpServletResponse response = null;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
DesignationListXLSReport xlsReport = null;
@SuppressWarnings("static-access")
public String execute() throws Exception {
try {
connection = getConnection();
String sql = "select * from tab_user_type";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
int currentRow = 1;
HSSFRow row;
// Writing Data to ExcelSheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet spreadSheet = wb.createSheet("User Type List");
row = spreadSheet.createRow(0);
// This is for Header Style
HSSFCellStyle headerCellStyle = wb.createCellStyle();
headerCellStyle.setFillForegroundColor(HSSFColor.BROWN.index);
headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont setFont = wb.createFont();
setFont.setFontHeightInPoints((short) 10);
setFont.setColor(HSSFColor.WHITE.index);
setFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerCellStyle.setBorderBottom(headerCellStyle.BORDER_THIN);
headerCellStyle.setFont(setFont);
// This is for Data Style
HSSFCellStyle dataCellStyle = wb.createCellStyle();
HSSFFont setDataFont = wb.createFont();
setDataFont.setColor(HSSFColor.LIGHT_BLUE.index);
dataCellStyle.setBorderBottom(dataCellStyle.BORDER_THIN);
dataCellStyle.setFont(setDataFont);
HSSFCell cell = null;
spreadSheet.setColumnWidth((short) 0, (short) (256 * 25));
spreadSheet.setColumnWidth((short) 1, (short) (256 * 25));
spreadSheet.setColumnWidth((short) 2, (short) (256 * 25));
spreadSheet.setColumnWidth((short) 3, (short) (256 * 25));
spreadSheet.setColumnWidth((short) 4, (short) (256 * 25));
cell = row.createCell((short) 0);
cell.setCellValue("User Type ID");
cell.setCellStyle(headerCellStyle);
cell = row.createCell((short) 1);
cell.setCellValue("User Type Name");
cell.setCellStyle(headerCellStyle);
cell = row.createCell((short) 2);
cell.setCellValue("User Type Desc");
cell.setCellStyle(headerCellStyle);
cell = row.createCell((short) 3);
cell.setCellValue("Created Date");
cell.setCellStyle(headerCellStyle);
cell = row.createCell((short) 4);
cell.setCellValue("Created By");
cell.setCellStyle(headerCellStyle);
List<DesignationListXLSReport> li = new ArrayList<DesignationListXLSReport>();
while (resultSet.next()) {
xlsReport = new DesignationListXLSReport();
// create a row in the spreadsheet
row = spreadSheet.createRow(currentRow++);
cell = row.createCell((short) 0);
cell.setCellValue(resultSet.getString("USER_TYPE_ID"));
cell.setCellStyle(dataCellStyle);
cell = row.createCell((short) 1);
cell.setCellValue(resultSet.getString("USER_TYPE_NAME"));
cell.setCellStyle(dataCellStyle);
cell = row.createCell((short) 2);
cell.setCellValue(resultSet.getString("USER_TYPE_DESC"));
cell.setCellStyle(dataCellStyle);
cell = row.createCell((short) 3);
cell.setCellValue(resultSet.getString("CREATED_DATE"));
cell.setCellStyle(dataCellStyle);
cell = row.createCell((short) 4);
cell.setCellValue(resultSet.getString("CREATED_BY"));
cell.setCellStyle(dataCellStyle);
li.add(xlsReport);
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("UserType_list.xls");
wb.write(fileOut);
fileOut.close();
resultSet.close();
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
}
答案 0 :(得分:0)
如果代码全部在网络服务器上,您可以使用.htaccess。找到.htaccess文件(或创建它)并添加:
AddType application/octet-stream xls
// Change the last argument to xlsx if the Excel file is post-2003 MS Office version
这将强制浏览器将文件解释为下载,这将打开“另存为”对话框。
希望这会有所帮助。 -CE