繁体   English   中英

单击锚标记以保存pdf或excel文件后,打开“保存”对话框

[英]Open Save dialog box after clicking anchor tag for saving pdf or excel file

我正在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类是:

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";
    }

}

如果代码全部在网络服务器上,则可以使用.htaccess。 查找(或创建).htaccess文件并添加:

AddType application/octet-stream xls
// Change the last argument to xlsx if the Excel file is post-2003 MS Office version

这将强制浏览器将文件解释为下载文件,这将打开“另存为”对话框。

希望这可以帮助。 -CE

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM