我能够在服务器系统中生成 excel 文件,但问题是每当我尝试从客户端系统访问它时,它只会在服务器系统中生成,而不是在客户端系统中。以下代码用于生成 excel 文件:
<%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.swing.JFileChooser" %>
<%@ page import="java.awt.Desktop"%>
<%@ page isErrorPage='true' %>
<%!
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "s";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String userPassword = "s";
%>
<br><br>
<%
java.util.Date date = new java.util.Date();
JFileChooser chooseFile=new JFileChooser();
chooseFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooseFile.setDialogTitle("Select a Directory");
chooseFile.showDialog(null,"Click Me to Save the Folder");
//String filename = "/tmp/Excel "+System.currentTimeMillis() +".xls" ;
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,username,userPassword);
Statement stmt = conn.createStatement();
String strQuery = "select * from Meter_List";
ResultSet rs = stmt.executeQuery(strQuery);
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
HSSFRow rowhead = sheet.createRow((short)2);
rowhead.createCell(0).setCellValue("SNo");
rowhead.createCell(1).setCellValue("Meterid");
rowhead.createCell(2).setCellValue("Consumerid");
rowhead.createCell(3).setCellValue("Consumername");
rowhead.createCell(4).setCellValue("LastReading");
rowhead.createCell(5).setCellValue("Date");
rowhead.createCell(6).setCellValue("Time");
rowhead.createCell(7).setCellValue("Status");
rowhead.createCell(8).setCellValue("Subzone");
rowhead.createCell(9).setCellValue("Zone");
int index=3;
int sno=0;
String name="";
while(rs.next())
{
sno++;
HSSFRow row = sheet.createRow((short)index);
row.createCell(0).setCellValue(sno);
row.createCell(1).setCellValue(rs.getInt("Meterid"));
row.createCell(2).setCellValue(rs.getInt("Consumerid"));
row.createCell(3).setCellValue(rs.getString("Consumername"));
row.createCell(4).setCellValue(rs.getInt("lastreading"));
row.createCell(5).setCellValue(rs.getDate("Date"));
row.createCell(6).setCellValue(rs.getTime("Time"));
row.createCell(7).setCellValue(rs.getString("Status"));
row.createCell(8).setCellValue(rs.getString("Subzone"));
row.createCell(9).setCellValue(rs.getString("Zone"));
index++;
}
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
out.println("<b>Opening worksheet, please wait......</b><br>");
Desktop dt=Desktop.getDesktop();
dt.open(new File(filename));
out.println("<b>Worksheet opened. It is saved as -\n\t\t </b><br>"+filename);
}
catch ( Exception ex )
{
//out.println("Error :: "+ex);
out.println("");
}
%>
can you please help to solve this.