Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击用于保存pdf或excel文件的定位标记后,打开“保存”对话框_Java_Jsp_Struts2 - Fatal编程技术网

Java 单击用于保存pdf或excel文件的定位标记后,打开“保存”对话框

Java 单击用于保存pdf或excel文件的定位标记后,打开“保存”对话框,java,jsp,struts2,Java,Jsp,Struts2,我在struts2.0中的项目中工作,我想通过保存对话框在特定位置保存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" clas

我在struts2.0中的项目中工作,我想通过保存对话框在特定位置保存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>

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

}
import java.io.FileOutputStream;
导入java.sql.Connection;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.util.ArrayList;
导入java.util.List;
导入javax.servlet.http.HttpServletResponse;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFCellStyle;
导入org.apache.poi.hssf.usermodel.hssfont;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.hssf.util.HSSFColor;
导入org.gst.gspf.core.framework.jdbc.JdbcDAOSupport;
公共类指定ListXLsReport扩展了JDBCDAO支持{
HttpServletResponse=null;
连接=空;
PreparedStatement PreparedStatement=null;
ResultSet ResultSet=null;
指定ListXLsReport xlsReport=null;
@抑制警告(“静态访问”)
公共字符串execute()引发异常{
试一试{
connection=getConnection();
String sql=“从选项卡\用户\类型中选择*”;
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
int currentRow=1;
HSSFRow row;
//将数据写入Excel工作表
HSSFWorkbook wb=新的HSSFWorkbook();
HSSFSheet电子表格=wb.createSheet(“用户类型列表”);
行=电子表格。创建行(0);
//这是标题样式
HSSFCellStyle headerCellStyle=wb.createCellStyle();
headerCellStyle.setFillForegroundColor(HSSFColor.BROWN.index);
headerCellStyle.setFillPattern(HSSFCellStyle.SOLID\u前景);
hssfont setFont=wb.createFont();
setFont.setFontHeightInPoints((短)10);
setFont.setColor(HSSFColor.WHITE.index);
setFont.setBoldweight(hssfont.BOLDWEIGHT\u BOLD);
headerCellStyle.setBorderBottom(headerCellStyle.BORDER_THIN);
headerCellStyle.setFont(setFont);
//这是针对数据样式的
HSSFCellStyle dataCellStyle=wb.createCellStyle();
hssfont setDataFont=wb.createFont();
setDataFont.setColor(HSSFColor.LIGHT_BLUE.index);
dataCellStyle.setBorderBottom(dataCellStyle.BORDER_THIN);
setFont(setDataFont);
HSSFCell单元=空;
电子表格.setColumnWidth((短)0,(短)(256*25));
电子表格.setColumnWidth((短)1,(短)(256*25));
电子表格.setColumnWidth((短)2,(短)(256*25));
电子表格.setColumnWidth((短)3,(短)(256*25));
电子表格.setColumnWidth((短)4,(短)(256*25));
cell=row.createCell((短)0);
cell.setCellValue(“用户类型ID”);
cell.setCellStyle(headerCellStyle);
cell=row.createCell((短)1);
cell.setCellValue(“用户类型名称”);
cell.setCellStyle(headerCellStyle);
cell=row.createCell((短)2);
cell.setCellValue(“用户类型描述”);
cell.setCellStyle(headerCellStyle);
cell=row.createCell((短)3);
cell.setCellValue(“创建日期”);
cell.setCellStyle(headerCellStyle);
cell=row.createCell((短)4);
cell.setCellValue(“创建人”);
cell.setCellStyle(headerCellStyle);
List li=new ArrayList();
while(resultSet.next()){
xlsReport=新名称列表xlsReport();
//在电子表格中创建一行
行=电子表格.createRow(currentRow++);
cell=row.createCell((短)0);
cell.setCellValue(resultSet.getString(“用户类型\ ID”);
cell.setCellStyle(dataCellStyle);
cell=row.createCell((短)1);
cell.setCellValue(resultSet.getString(“用户类型\名称”);
cell.setCellStyle(dataCellStyle);
cell=row.createCell((短)2);
cell.setCellValue(resultSet.getString(“用户类型描述”);
cell.setCellStyle(dataCellStyle);
cell=row.createCell((短)3);
cell.setCellValue(resultSet.getString(“创建日期”);
cell.setCellStyle(dataCellStyle);
cell=row.createCell((短)4);
cell.setCellValue(resultSet.getString(“创建人”);
cell.setCellStyle(dataCellStyle);
li.添加(xlsReport);
}
//将输出写入文件
FileOutputStream fileOut=新的FileOutputStream(“UserType_list.xls”);
wb.写入(文件输出);
fileOut.close();
resultSet.close();
preparedStatement.close();
connection.close();
}捕获(例外e){
e、 printStackTrace();
}
返回“成功”;
}
}

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

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


希望这有帮助-CE

您应该通过将所需文件的数据写入到中来触发文件下载
AddType application/octet-stream xls
// Change the last argument to xlsx if the Excel file is post-2003 MS Office version