我正在使用GWT应用程序。当我单击按钮时,我将请求参数传递给数据库并获取结果。使用此结果集我想创建excel并且必须为用户提供下载弹出窗口。 Excel文件正在创建但不是弹出窗口 代码如下:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import com.excelexporter.shared.ResponseObj;
public class ExportExcel extends HttpServlet {
private static final long serialVersionUID = 1L;
private static ObjectInputStream inObj;;
private String encoding = "ISO-8859-1";
List<ResponseObj> implRes = new ArrayList<ResponseObj>();
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
inObj = new ObjectInputStream(req.getInputStream());
try {
HSSFWorkbook workbook = new HSSFWorkbook();
//Create a blank sheet
HSSFSheet sheet = workbook.createSheet("Employee Data");
implRes = (List<ResponseObj>)inObj.readObject();
Map<Integer, Object[]> data = new TreeMap<Integer, Object[]>();
data.put(1, new Object[] {"ID", "Mark_1", "Mark_2", "Mark_3","Mark_4", "Mark_5"});
String html = "html";
String fileName = "Export.xls";
if (!fileName.endsWith(".xls")) {
fileName = fileName + ".xls";
}
resp.setContentType("application/vnd.ms-excel");// magic is here
resp.setHeader("Content-Disposition", "attachment; filename=test.xls");
if(implRes!=null){
for(int i=0;i<implRes.size();i++){
data.put(i+2, new Object[] {i+1, implRes.get(i).getMark_1(),
implRes.get(i).getMark_1(),
implRes.get(i).getMark_1(),
implRes.get(i).getMark_1(),
implRes.get(i).getMark_1()});
}
}
Set<Integer> keyset = data.keySet();
int rownum = 0;
for (int key : keyset)
{
Row row = sheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr)
{
Cell cell = row.createCell(cellnum++);
if(obj instanceof String)
cell.setCellValue((String)obj);
else if(obj instanceof Integer)
cell.setCellValue((Integer)obj);
}
}
//FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
OutputStream out = resp.getOutputStream();
workbook.write(out);
out.close();
workbook.close();
System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
我正在使用以下代码调用上面的servlet:
public List<ResponseObj> greetServer(Requester myReq) throws IllegalArgumentException {
if(myReq.getUserName().equalsIgnoreCase("Admin")){
for(int i =1;i<=100;i++){
myRes = new ResponseObj();
myRes.setMark_1(new Integer(10*i).toString());
myRes.setMark_2(new Integer(30*i).toString());
myRes.setMark_3(new Integer(50*i).toString());
myRes.setMark_4(new Integer(70*i).toString());
myRes.setMark_5(new Integer(90*i).toString());
res.add(myRes);
}
}
try {
url = new URL("http://127.0.0.1:8888/excelexporter/excel?a=res");
con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setDefaultUseCaches(false);
con.setRequestProperty("Content-Type", "application/octet-stream");
objOut = new ObjectOutputStream(con.getOutputStream());
objOut.writeObject(res);
objIn = new ObjectInputStream(con.getInputStream());
String status = (String)objIn.readObject();
System.out.println("Hello Status " +status);
objIn.close();
objOut.flush();
objOut.close();
} catch (Exception e) {
GWT.log("Warn#####");
}
return res;
}