我是在tomcat5.0环境下做的poi读取上传的excel文件
如果没有中文,可以正常运行,
如果excel里有中文,就显示??
如果上传的文件名带中文,就不能读到文件。
这个问题该怎么解决?
如果没有中文,可以正常运行,
如果excel里有中文,就显示??
如果上传的文件名带中文,就不能读到文件。
这个问题该怎么解决?
解决方案 »
- SSh hibernate 执行hql报错:org.springframework.orm.hibernate3.HibernateQueryException
- struts框架网页内使用js验证,但不能显示bean:message 中对应的key值
- 求JSP页面,从SESSION里取到一个值,转成百分数在页面显示的JS代码
- 请求Struts好的资源?
- 请问在服务器端怎么判断客户端使用代理?
- 本人想了解web版本代理IP设置知识
- 我要提问
- 传值问题,为什么传不过去啊??在线=待.... 续....
- 求教啊,为什么不能增加中文到数据库里.
- 新手提问,如何让我的servlet运行起来。
- commons-fileupload 1.1 有没限制上传文件格式的方法?
- jsp页面中参数的传递
throws IOException {
...
response.setContentType("application/vnd.ms-excel;charset=gb2312");
response.setCharacterEncoding("UTF-8");
//------
String docName="电流极值报表.xls";
docName=java.net.URLEncoder.encode(docName,"UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" +new String(docName.getBytes("UTF-8"),"GBK"));
//------
我刚遇见,绝对可以解决,最好名字不要太长!
问题应该是servlet整个不支持中文
我在里面打2个中文都显示??
response.setContentType("application/vnd.ms-excel;charset=gb2312");
这句什么意思?以excel显示?那样的话就弹出打开或保存的对话框了。
我把原始代码贴出来,哪给高手指点下,多谢了
package com;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Enumeration;public class POIExcelServlet extends HttpServlet
{
//从excel中读取数据存入ArrayList中
String pp ="F:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\ROOT\\Excel\\";
String poiexcelPath = "";public void init(ServletConfig config) throws ServletException{
super.init(config);
}protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
doPost(request,response);
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
String fn=request.getParameter("filename");
poiexcelPath = pp + fn;
readExcel(this.poiexcelPath,response);
}public void readExcel(String filename,HttpServletResponse response) throws IOException
{
DecimalFormat df = new DecimalFormat("#");
String fileToBeRead = filename;
PrintWriter out = response.getWriter();
try{
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
int numSheets;
int rowNumOfSheet;
short cellNumOfRow;
for(numSheets = 0;numSheets < workbook.getNumberOfSheets();numSheets++)
{
if(null != workbook.getSheetAt(numSheets))
{
HSSFSheet aSheet = workbook.getSheetAt(numSheets);
for (rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum();rowNumOfSheet++)
{
if(null != aSheet.getRow(rowNumOfSheet)){
HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
for (cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum();cellNumOfRow++)
{
if(aRow.getCell(cellNumOfRow)!=null){
HSSFCell aCell = aRow.getCell(cellNumOfRow);
int cellType = aCell.getCellType();
switch(cellType){
case 0:
String strCell = df.format(aCell.getNumericCellValue());
out.println(strCell+ "");
break;
case 1:
strCell = aCell.getStringCellValue();
out.println(strCell+ "");
break;
default:
strCell = "数据格式不对";
out.println(strCell+ "");
} }
}
out.print("<br>");
}
}
}
}
}catch(Exception e){
System.out.println("ReadExcelError"+ e);
}
}
}
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
String fn=request.getParameter("filename");
往里面插入中文,再将此文件上传上去,可以识别中文
但直接建立的excel表,里面的中文却还是不能显示
问题是不是excel跟tomcat的编码不同的缘故呢?