用poi导出Excel表格,但是当我运行程序的时候不是默认用Excel打开,并且选择用Excel打开之后还不出数据。 50
下面是代码,麻烦帮我看一下谢谢<%request.setCharacterEncoding("UTF-8");//iRet为1则导出地区报告表,2则导出行业报告表Stri...
下面是代码,麻烦帮我看一下
谢谢
<%
request.setCharacterEncoding("UTF-8");
//iRet为1则导出地区报告表,2则导出行业报告表
String iRet = request.getParameter("iRet");
response.setContentType("application/vnd.ms-excel; charset=UTF-8");
String fname;
if (iRet.equals("1")) {
fname = "地区报告表";
} else {
fname = "行业报告表";
}
response.addHeader("Content-Disposition", "inline;filename="+ fname + ".xls");
//New Workbook
HSSFWorkbook wb = new HSSFWorkbook();
//New Sheet
HSSFSheet sheet;
if (iRet.equals("1")) {
sheet = wb.createSheet("地区报告表");
} else {
sheet = wb.createSheet("行业报告表");
}
sheet.setDefaultColumnWidth((short) 15);
//数据库连接,进行相关查询操作
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Connection con;
DriverManagerDataSource ds = new DriverManagerDataSource();
//具体的链接参数。。。
con = ds.getConnection();
Statement stmt = con.createStatement();
String sql = "";
if (iRet.equals("1")) {
sql = "SELECT t3.city,count(*) "
+ " FROM ups_company_person as t1,ups_ex_company as t2,ups_city as t3 "
+ " WHERE t1.company_id=t2.CompanyID "
+ " and t2.Province=t3.id " + " group by Province "
+ " order by count(*) desc";
} else {
sql = "SELECT vote_option, count(*) "
+ "FROM ups_investigate_content AS con, ups_indagate_result AS res "
+ "WHERE con.title_id =3 "
+ "AND con.vote_code = res.vote_code "
+ "GROUP BY con.vote_code";
}
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("clo1", rs.getString(1));
map.put("clo2", rs.getString(2));
list.add(map);
}
rs.close();
stmt.close();
con.close();
HSSFRichTextString text;
HSSFRow row = sheet.createRow(0);
if (iRet.equals("1")) {
text=new HSSFRichTextString("地区");
} else {
text=new HSSFRichTextString("行业");
}
row.createCell(1).setCellValue(text);
row.createCell(2).setCellValue("数量");
//begin
int i = 1;
if (list != null) {
for (HashMap<String, String> haspMap : list) {
row = sheet.createRow(i);
row.createCell(1).setCellValue(haspMap.get("clo1"));
row.createCell(2).setCellValue(haspMap.get("clo2"));
i++;
}
}
//end
wb.setPrintArea(0, "$A$1:$C$2");
FileOutputStream fileOut = new FileOutputStream(fname);
wb.write(fileOut);
fileOut.close();
%> 展开
谢谢
<%
request.setCharacterEncoding("UTF-8");
//iRet为1则导出地区报告表,2则导出行业报告表
String iRet = request.getParameter("iRet");
response.setContentType("application/vnd.ms-excel; charset=UTF-8");
String fname;
if (iRet.equals("1")) {
fname = "地区报告表";
} else {
fname = "行业报告表";
}
response.addHeader("Content-Disposition", "inline;filename="+ fname + ".xls");
//New Workbook
HSSFWorkbook wb = new HSSFWorkbook();
//New Sheet
HSSFSheet sheet;
if (iRet.equals("1")) {
sheet = wb.createSheet("地区报告表");
} else {
sheet = wb.createSheet("行业报告表");
}
sheet.setDefaultColumnWidth((short) 15);
//数据库连接,进行相关查询操作
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Connection con;
DriverManagerDataSource ds = new DriverManagerDataSource();
//具体的链接参数。。。
con = ds.getConnection();
Statement stmt = con.createStatement();
String sql = "";
if (iRet.equals("1")) {
sql = "SELECT t3.city,count(*) "
+ " FROM ups_company_person as t1,ups_ex_company as t2,ups_city as t3 "
+ " WHERE t1.company_id=t2.CompanyID "
+ " and t2.Province=t3.id " + " group by Province "
+ " order by count(*) desc";
} else {
sql = "SELECT vote_option, count(*) "
+ "FROM ups_investigate_content AS con, ups_indagate_result AS res "
+ "WHERE con.title_id =3 "
+ "AND con.vote_code = res.vote_code "
+ "GROUP BY con.vote_code";
}
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("clo1", rs.getString(1));
map.put("clo2", rs.getString(2));
list.add(map);
}
rs.close();
stmt.close();
con.close();
HSSFRichTextString text;
HSSFRow row = sheet.createRow(0);
if (iRet.equals("1")) {
text=new HSSFRichTextString("地区");
} else {
text=new HSSFRichTextString("行业");
}
row.createCell(1).setCellValue(text);
row.createCell(2).setCellValue("数量");
//begin
int i = 1;
if (list != null) {
for (HashMap<String, String> haspMap : list) {
row = sheet.createRow(i);
row.createCell(1).setCellValue(haspMap.get("clo1"));
row.createCell(2).setCellValue(haspMap.get("clo2"));
i++;
}
}
//end
wb.setPrintArea(0, "$A$1:$C$2");
FileOutputStream fileOut = new FileOutputStream(fname);
wb.write(fileOut);
fileOut.close();
%> 展开
3个回答
展开全部
POI很难用的,建议你用openoffice
追问
我之前已经用jxl写过一次了,但是合代码的时候是乱码。
所以老板说在用poi写一下试一试。
现在用poi写已经没问题了,并且在tomcat下也能成功的导出Excel.
可是换成resin服务器就不行了,导出来的时候Excel文件的名字是中文的,没问题
但是内容却是乱码。。。。
请问您知道应该怎么解决吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
到excel home提问专业的更多
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询