// 文件的存放目录
private static final String PATH_FILE = System
.getProperty("java.io.tmpdir"); // 文件名(用户登录人的ID作为文件名)
private static final String NAME_FILE = LoginManager.getInstance()
.getPerson().getId()
+ ".tmp"; /**
 * 创建生成Excel文件
 * 
 * @param title
 * @param o
 * @throws IOException
 */
public static void createExcel(String title, Object[][] o)
throws IOException {
if (o == null)
throw new IllegalArgumentException("Null 数据对象!");
if (o.length == 0)
throw new IllegalArgumentException("标题数据为空!"); // 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
HSSFSheet sheet = workbook.createSheet(title); // 1.样式的设置
HSSFCellStyle style = workbook.createCellStyle();
{
// 边框
style.setBorderTop((short) 1);
style.setBorderBottom((short) 1);
style.setBorderLeft((short) 1);
style.setBorderRight((short) 1);
}

// 在第一列中加入标题
HSSFRow row = sheet.createRow((short) 0);
sheet.addMergedRegion(new Region(0, (short) 0, 0,
(short) (o[0].length - 1))); HSSFCell cell = row.createCell((short) 0);
{
// 添加字符串
// cell.setCellValue(new HSSFRichTextString(title));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设定utf-16中文字显示
cell.setCellValue(title);
cell.setCellStyle(style);
} // 加入数据
for (int i = 0, iSize = o.length; i < iSize; i++) {
row = sheet.createRow((short) (i + 1));
for (int j = 0, jSize = o[i].length; j < jSize; j++) {

cell = row.createCell((short) (j));

// 添加字符串
// if(o[i][j] instanceof String)
// cell.setCellValue(new
// HSSFRichTextString(o[i][j].toString()));
if (o[i][j] != null ){
cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设定utf-16中文字显示
cell.setCellValue(o[i][j].toString());
cell.setCellStyle(style);
}
}
} // 新建一输出文件流
OutputStream fOut = new FileOutputStream(PATH_FILE
+ java.io.File.separator + NAME_FILE);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
}
下载时点击打开,出现提示“excel在XXXX.xls中发现不可读取的内容,是否恢复工作簿的内容?如果信任此工作簿的来源,请单击是”点击是后可查看正确的内容,但我在服务器上点击这个文件时不会提示这个,
另附MS的错误提示<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error029680_01.xml</logFileName><summary>在文件“C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files\Content.IE5\M7AFAH6F\人员一览091229[1].xls”中检测到错误</summary><additionalInfo><info>重新命名无效的工作表名称。</info></additionalInfo></recoveryLog>另附下载代码
public class DownloadAction extends org.apache.struts.actions.DownloadAction { @Override
protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
final LoginManager loginManager = LoginManager.getInstance(); String fileName = request.getParameter("fname"); // 设置日期
final SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
fileName = fileName + sdf.format(new Date());
//防止乱码  
fileName=new String(fileName.getBytes("gbk"),"iso8859-1");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName.concat(".xls"));// 设置文件名称 StreamInfo sInfo = null;   // 磁盘下载
// 文件类型
String contentType = "application/octet-stream"; // 文件路径
String path = System.getProperty("java.io.tmpdir") + File.separator
+ loginManager.getPerson().getId() + ".tmp"; File file = new File(path); sInfo = new FileStreamInfo(contentType, file); return sInfo;
}
}求高手指点一二!

解决方案 »

  1.   


    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <logFileName>error029680_01.xml</logFileName>
    <summary>在文件“C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files\Content.IE5\M7AFAH6F\人员一览091229[1].xls”中检测到错误</summary>
    <additionalInfo>
    <info>重新命名无效的工作表名称。
    </info>
    </additionalInfo>
    </recoveryLog>
      

  2.   

    原因是使用的是2007打开的此文档,而使用的POI包版本还不支持07,使用03打开不存在此问题。目前的POI包版本已可对2007进行操作!