日期:2014-05-17  浏览次数:20660 次

关于导出数据库中的数据到EXCEL表格的setEncoding报错的问题
本人初学web开发,现在想在JSP页面中添加将数据库中的内容导入到excel表格的功能。在网上看了一些例子,其中有用poi的,我把原代码拷下来自己试,结果发现代码中报错:
Multiple markers at this line
- The method setEncoding(short) is undefined for the type 
HSSFCell
- The method setEncoding(short) is undefined for the type 
Object
请问这是什么原因?
完整代码如下:
package myBean;

import org.apache.poi.hssf.usermodel.*;
import java.io.*;
import java.sql.*;
import myBean.DBConnection;

public class POIExcel {
private DBConnection con;

public POIExcel() {
con=new DBConnection();
}
//@SuppressWarnings("deprecation")
@SuppressWarnings("deprecation")
public void exportExcel(String name,OutputStream out){
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet(name);

HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓名");

cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("电子邮件");

String sql="select * from tb_contacts";
int nRow=1;
try{
ResultSet rs=con.execQuery(sql);
if(rs!=null)
while(rs.next()){
row=sheet.createRow(nRow++);
cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("name"));

cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(rs.getString("email"));
}
rs.close();
}catch(SQLException e){
System.out.println("POIExcel.exportExcel()"+e.getMessage());
}
try{
wb.write(out);
}catch(Exception e){
System.out.println("POIExcel.exportExcel()"+e.getMessage());
}
}
}


------解决方案--------------------
POI用的是哪版本?我用的3.6setEncoding()方法已经删掉了。
lz可以考虑换旧一点的版本