http://wenku.baidu.com/link?url=nGUizQx0Xgp1nIftAr6FmGL2DZi_-psODgsH5-CHQ7fHW0CgPWTVDiT-46xajw5RZeV2_ZCcj3jbTPRKxEA5X8_zm5gIE7r22295puHmzIK

解决方案 »

  1.   

    package reader;import java.io.File;
    import java.io.IOException;import jxl.Cell;
    import jxl.CellType;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;public class ReadExcel {  private String inputFile;  public void setInputFile(String inputFile) {
        this.inputFile = inputFile;
      }  public void read() throws IOException  {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try {
          w = Workbook.getWorkbook(inputWorkbook);
          // Get the first sheet
          Sheet sheet = w.getSheet(0);
          // Loop over first 10 column and lines      for (int j = 0; j < sheet.getColumns(); j++) {
            for (int i = 0; i < sheet.getRows(); i++) {
              Cell cell = sheet.getCell(j, i);
              CellType type = cell.getType();
              if (type == CellType.LABEL) {
                System.out.println("I got a label "
                    + cell.getContents());
              }          if (type == CellType.NUMBER) {
                System.out.println("I got a number "
                    + cell.getContents());
              }        }
          }
        } catch (BiffException e) {
          e.printStackTrace();
        }
      }  public static void main(String[] args) throws IOException {
        ReadExcel test = new ReadExcel();
        test.setInputFile("c:/temp/lars.xls");
        test.read();
      }} 用这个试试。参考
    http://www.vogella.com/articles/JavaExcel/article.html
      

  2.   


    JSP两行代码导出excel /word 笔记
    在正常的展示列表页面添加两行代码即可导出excel 电子表格 :
     html标签前加入如下代码:
    <%@ page contentType="application/msexcel" %>
    <!-- 以上这行设定本网页为excel格式的网页 -->
    <%
       response.setHeader("Content-disposition","inline; filename=agreement.xls");
       //以上这行设定传送到前端浏览器时的档名为agreement.xls
       //就是靠这一行,让前端浏览器以为接收到一个excel档 
    //导出Word只需要把contentType="application/msexcel"改为contentType="application/msword" ,filename=agreement.xls 改成 filename=agreement.doc  
    %>
    格式化excel值:
    用法: 
    在css中加入:mso-number-format定义数据格式,格式可以在excel中查看自定义格式,具体可以参考一下:
    mso-number-format:"0" NO Decimals 
    mso-number-format:"0\.000" 3 Decimals 
    mso-number-format:"\#\,\#\#0\.000" Comma with 3 dec 
    mso-number-format:"mm\/dd\/yy" Date7 
    mso-number-format:"mmmm\ d\,\ yyyy" Date9 
    mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM" D -T AMPM 
    mso-number-format:"Short Date" 01/03/1998 
    mso-number-format:"Medium Date" 01-mar-98 
    mso-number-format:"d\-mmm\-yyyy" 01-mar-1998 
    mso-number-format:"Short Time" 5:16 
    mso-number-format:"Medium Time" 5:16 am 
    mso-number-format:"Long Time" 5:16:21:00 
    mso-number-format:"Percent" Percent - two decimals 
    mso-number-format:"0%" Percent - no decimals 
    mso-number-format:"0\.E+00" Scientific Notation 
    mso-number-format:"\@" Text 
    mso-number-format:"\#\ ???\/???" Fractions - up to 3 digits (312/943) 如果项目放到weblogic 服务器下 excel可以正常导出,但有时候会报“  Response already committed” 异常,原因可能是:
    重复提交,在某个地方有submit等的信息
    处理办法:可以使用类似 out.clear() 的语句来关闭某些流
    用法:放置与最后一个html结束标签之后
    实例代码 :
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page contentType="application/msexcel"%>    <!-- 以上这行设定本网页为excel格式的网页 -->
    <%
    response.setHeader("Content-disposition","inline; filename=daycheak.xls");  <!-- 这行设定传送到前端浏览器时的档名为daycheak.xls 就是靠这一行,让前端浏览器以为接收到一个excel档 -->
    %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
    %>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>"><title>My JSP 'daycheak.jsp' starting page</title><meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    --></head><body>
    <TABLE width="1000" height="118" border="1">
    <tr>
    <td colspan="8" align="center" valign="middle">
    日终对账记录
    </td>
    </tr>
    <tr>
    <th width="8%" align="center" valign="middle" scope="col">
    序号
    </th>
    <th width="20%" align="center" valign="middle" scope="col">
    日期
    </th>
    <th width="15%" align="center" valign="middle" scope="col">
    业务种类
    </th>
    <th width="15%" align="center" valign="middle" scope="col">
    业务发起方
    </th>
    <th width="15%" align="center" valign="middle" scope="col">
    业务接收方
    </th>
    <th width="8%" align="center" valign="middle" scope="col">
    交易金额
    </th>
    <th width="8%" align="center" valign="middle" scope="col">
    业务笔数
    </th>
    <th width="8%" align="center" valign="middle" scope="col">
    交易状态
    </th>
    </tr>
    <s:iterator value="#request.daycheakList" var="item" status="st">
    <tr>
    <td align="center" valign="middle" style="mso-number-format: '\@';">     <!--style="mso-number-format: '\@';" : 格式化excel单元值为文本 -->
    ${st.count}
    </td>
    <td align="center" valign="middle" style="mso-number-format: '\@';">
    ${item.DAY}
    </td>
    <td align="center" valign="middle" style="mso-number-format: '\@';">
    ${item.BUSINESSNAME}
    </td>
    <td align="center" valign="middle" style="mso-number-format: '\@';">
    ${item.FROMCODE}
    </td>
    <td align="center" valign="middle" style="mso-number-format: '\@';">
    ${item.TOCODE}
    </td>
    <td align="center" valign="middle" style="mso-number-format: '\@';">
    ${item.BUSINESSMONEY}
    </td>
    <td align="center" valign="middle" style="mso-number-format: '\@';">
    ${item.BUSINESSCOUNT}
    </td>
    <td align="center" valign="middle" style="mso-number-format: '\@';">
    ${item.STATUS ==0 ?'成功':'失败' }   <!--structs2 三元表达式的使用 -->
    </td>
    </tr>
    </s:iterator>
    </table></body>
    </html>
    <% out.clear();%>    <!-- 处理页面因为重复提交可能引发的 “Response already committed ”异常-->
      

  3.   

    直接请求到jsp页面
    <%@ page language="java" contentType="text/html; charset=GBK"
            pageEncoding="GBK"%>
    <%@ page import="java.util.*"%>
    <%@ page import="java.io.*"%>
    <%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Export Excel</title>
    </head>
    <body>
    <%
            
                    response.setContentType("application/msexcel");
                    response.setHeader("Content-disposition",
                                    "inline; filename=data.xls"); //attachment
                    DownExcel excel = new DownExcel();
                    HSSFWorkbook wb = excel.downExcel(deviceType);
                    OutputStream os = response.getOutputStream();
                    wb.write(os);
                    os.flush();
                    os.close();
                    out.clear();
                    out = pageContext.pushBody();        } 
    </body>import java.util.List;
    import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;import com.cemso.dto.DeviceDTO;
    import com.cemso.service.DeviceService;
    import com.cemso.service.impl.DeviceServiceImpl;public class DownExcel {        public HSSFWorkbook downExcel(String deviceType) {
                    HSSFWorkbook wb = new HSSFWorkbook();
                    HSSFSheet sheet = wb.createSheet();                HSSFRow row = sheet.createRow(0);
                    HSSFCell cell = row.createCell(0);
                    cell.setCellValue("名称");
                    cell = row.createCell(1);
                    cell.setCellValue("局域网IP");
                    cell = row.createCell(2);
                    cell.setCellValue("在线状态");
                    cell = row.createCell(3);
                    cell.setCellValue("启动时间");
                    cell = row.createCell(4);
                    cell.setCellValue("下次关机时间");
                    cell = row.createCell(5);
                    cell.setCellValue("当前播放播表");                DeviceService deviceService = new DeviceServiceImpl();                Map<String, List<DeviceDTO>> statedDeviceMap = deviceService
                                    .getStatedDevices();                if ("online".equals(deviceType)) {
                            List<DeviceDTO> exportedDevice = statedDeviceMap.get("online");
                            for (int i = 0; i < exportedDevice.size(); i++) {
                                    HSSFRow rowTemp = sheet.createRow(i + 1);
                                    DeviceDTO device = exportedDevice.get(i);
                                    HSSFCell cellTemp = rowTemp.createCell(0);
                                    cellTemp.setCellValue(device.getName());
                                    cellTemp = rowTemp.createCell(1);
                                    cellTemp.setCellValue(device.getIp());
                                    cellTemp = rowTemp.createCell(2);
                                    cellTemp.setCellValue(device.getOnlineState());
                                    cellTemp = rowTemp.createCell(3);
                                    cellTemp.setCellValue(device.getBootTime());
                                    cellTemp = rowTemp.createCell(4);
                                    cellTemp.setCellValue(device.getAutoShutdownTime());
                                    cellTemp = rowTemp.createCell(5);
                                    cellTemp.setCellValue(device.getCurrentPlaylist());
                            }
                    }
                    if ("offline".equals(deviceType)) {
                            List<DeviceDTO> exportedDevice = statedDeviceMap.get("offline");
                            for (int i = 0; i < exportedDevice.size(); i++) {
                                    HSSFRow rowTemp = sheet.createRow(i + 1);
                                    DeviceDTO device = exportedDevice.get(i);
                                    HSSFCell cellTemp = rowTemp.createCell(0);
                                    cellTemp.setCellValue(device.getName());
                                    cellTemp = rowTemp.createCell(1);
                                    cellTemp.setCellValue(device.getIp());
                                    cellTemp = rowTemp.createCell(2);
                                    cellTemp.setCellValue(device.getOnlineState());
                                    cellTemp = rowTemp.createCell(3);
                                    cellTemp.setCellValue(device.getBootTime());
                                    cellTemp = rowTemp.createCell(4);
                                    cellTemp.setCellValue(device.getAutoShutdownTime());
                                    cellTemp = rowTemp.createCell(5);
                                    cellTemp.setCellValue(device.getCurrentPlaylist());
                            }
                    }                // cell auto size
                    HSSFRow rows = sheet.getRow(0);
                    int coloumNum = rows.getPhysicalNumberOfCells();
                    for (int i = 0; i < coloumNum; i++) {
                            sheet.autoSizeColumn(i);
                    }                return wb;
            }
    }
      

  4.   

    不知道是我人品问题还是怎么了滴,,,我试了一下这个,一运行直接报错。
    注:${item.BUSINESSCOUNT}  我全换成了111111这种静态值。
      

  5.   

    http://download.csdn.net/detail/zhang527/4844652
      

  6.   

    不知道是我人品问题还是怎么了滴,,,我试了一下这个,一运行直接报错。
    注:${item.BUSINESSCOUNT}  我全换成了111111这种静态值。首先保证你的查询列表可以正常展示 ,之后再添加那两行代码 ,我给你的就是原版的,测试通过的 
      

  7.   

    根据pojo类的自省机制将excel模板中的数据读入到pojo类中,save保存
      

  8.   

    我能给你个Excel工具类,把数组转换成excel的形式。
    当然,具体还得需要你改改。。
    需要的话引用一下
      

  9.   

    不知道是我人品问题还是怎么了滴,,,我试了一下这个,一运行直接报错。
    注:${item.BUSINESSCOUNT}  我全换成了111111这种静态值。首先保证你的查询列表可以正常展示 ,之后再添加那两行代码 ,我给你的就是原版的,测试通过的 
    对不起啊,上次太草率了点。这样确实可以导出文件,可能我哪里出了问题吧。文件倒是导出了,可是里面没数据。我在看看
      

  10.   

    POI包,专门解析excel的,注意2003和2007版本