请高手指点一下
用jxls模板导出Excel
如何合并单元格?
最好能给个例子?
谢谢!

解决方案 »

  1.   

    合并单元格的例子:
    http://www.josdoc.com/html/Office-PDF/JXL/shenruyanjiu/200904/20-53.html
    其他jxl文档例子:
    http://www.josdoc.com/html/Office-PDF/JXL/
      

  2.   

    package com.ctc.ema.util;import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Date;import javax.sql.DataSource;import org.apache.log4j.Logger;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFDateUtil;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;import com.ctc.ema.init.Init;public class Excel extends DaoUtil {
    /** log4J配置 */
    private static Logger log = Logger.getLogger(Excel.class);
    /** 数据源 */
    private static DataSource ds = null; /*
     * 构造函数 初始化数据源
     */
    public Excel() {
    try {
    ds = Init.getDataSource();
    } catch (Exception ex) {
    log.error("", ex);
    }
    } // SQL语句带参数,返回结果对像
    public ResultSet getResultSet(String SQLString, String[] SQLparameter)
    throws Exception {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    conn = ds.getConnection();
    ps = conn.prepareStatement(SQLString);
    for (int i = 0; i < SQLparameter.length; i++) {
    ps.setObject(i + 1, SQLparameter[i]);
    }
    rs = ps.executeQuery();
    return rs;
    } // SQL语句不带参数,返回结果对像
    public ResultSet getResultSet(String SQLString) throws Exception {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    conn = ds.getConnection();
    ps = conn.prepareStatement(SQLString);
    rs = ps.executeQuery();
    return rs;
    } // 返回一个EXCEL的文档,此方法要传一个ResulSet,String[]参数,EXCEL的头标。
    @SuppressWarnings("deprecation")
    public HSSFWorkbook exprotExcel(String sql, String[] SQLparameter,
    String[] headString) throws Exception {

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("sheet1");
    // 以下以写表头
    // 表头为第一行
    HSSFRow row = null;
    HSSFCell cell = null;
    row = sheet.createRow((short) 0);
    for (int i = 0; i < headString.length; i++) {
    sheet.setColumnWidth((short) i, (short) 5000);
    cell = row.createCell((short) i);
    //cell.setEncoding((short) 1);
    cell.setCellType(1);
    cell.setCellValue(headString[i]);
    }
    ResultSet rs = null;
    rs = getResultSet(sql, SQLparameter);
    int i = 1;
    int sheetNumber = 2;
    try {
    while (rs.next()) {
    if(i > 60000) {
    sheet = wb.createSheet("sheet" + sheetNumber);
    sheetNumber++;
    i = 1;

    row = sheet.createRow(i);
    for (int j = 0; j < headString.length; j++) {
    cell = row.createCell((short) j);
    //cell.setEncoding((short) 1);
    cell.setCellType(1);
    cell.setCellValue(rs.getObject(j + 1) + "");
    }
    i++;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return wb;
    } // 返回一个EXCEL的文档,此方法要传一个SQL语句,Excel的头标。
    @SuppressWarnings("deprecation")
    public HSSFWorkbook exprotExcel(String sql, String[] headString)
    throws Exception { HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("sheet1");
    sheet.setColumnWidth((short) 30, (short) 0);
    // 以下以写表头
    // 表头为第一行
    HSSFRow row = null;
    HSSFCell cell = null;
    for (int i = 0; i < headString.length; i++) {
    sheet.setColumnWidth((short) i, (short) 5000);
    row = sheet.createRow((short) 0);
    cell = row.createCell((short) i);
    cell.setEncoding((short) 1);
    cell.setCellType(1);
    cell.setCellValue(headString[i]);
    }
    ResultSet rs = null;
    rs = getResultSet(sql);
    int i = 1;
    try {
    while (rs.next()) {
    row = sheet.createRow(i);
    for (int j = 0; j < headString.length; j++) {
    cell = row.createCell((short) j);
    cell.setEncoding((short) 1);
    cell.setCellType(1);
    cell.setCellValue(rs.getObject(j + 1) + "");
    }
    i++;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return wb;
    } // 返回一个EXCEL的文档,此方法要传一个ResulSet,EXCEL的头标。
    @SuppressWarnings("deprecation")
    public HSSFWorkbook exprotExcel(ResultSet rs, String[] headString)
    throws Exception { HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("sheet1");
    sheet.setColumnWidth((short) 30, (short) 0);
    // 以下以写表头
    // 表头为第一行
    HSSFRow row = null;
    HSSFCell cell = null;
    for (int i = 0; i < headString.length; i++) {
    sheet.setColumnWidth((short) i, (short) 5000);
    row = sheet.createRow((short) 0);
    cell = row.createCell((short) i);
    cell.setEncoding((short) 1);
    cell.setCellType(1);
    cell.setCellValue(headString[i]);
    }
    int i = 1;
    try {
    while (rs.next()) {
    row = sheet.createRow(i);
    // sheet.setColumnWidth((short)20,(short)20);
    for (int j = 0; j < headString.length; j++) { // row.setHeight((short)10);
    cell = row.createCell((short) j);
    cell.setEncoding((short) 1);
    cell.setCellType(1);
    cell.setCellValue(rs.getObject(j + 1) + "");
    }
    i++;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return wb;
    } @SuppressWarnings("deprecation")
    public String[] getStringArray(HSSFSheet sheet, int row) {
    // 根据行数取得Sheet的一行
    HSSFRow rowline = sheet.getRow(row);
    // 获取当前行的列数
    int filledColumns = rowline.getLastCellNum();
    String[] str = new String[filledColumns];
    HSSFCell cell = null;
    // 循环遍历所有列
    for (int i = 0; i < filledColumns; i++) {
    // 取得当前Cell
    cell = rowline.getCell((short) i);
    String cellvalue = null;
    if (cell != null) {
    // 判断当前Cell的Type
    switch (cell.getCellType()) {
    // 如果当前Cell的Type为NUMERIC
    case HSSFCell.CELL_TYPE_NUMERIC: {
    // 判断当前的cell是否为Date
    if (HSSFDateUtil.isCellDateFormatted(cell)) {
    // 如果是Date类型则,取得该Cell的Date值
    @SuppressWarnings("unused")
    Date date = cell.getDateCellValue();
    // 把Date转换成本地格式的字符串
    cellvalue = cell.getDateCellValue().toLocaleString();
    }
    // 如果是纯数字
    else {
    // 取得当前Cell的数值
    Integer num = new Integer((int) cell
    .getNumericCellValue());
    cellvalue = String.valueOf(num);
    }
    break;
    }
    // 如果当前Cell的Type为STRIN
    case HSSFCell.CELL_TYPE_STRING:
    // 取得当前的Cell字符串
    cellvalue = cell.getStringCellValue().replaceAll("'", "''");
    break;
    // 默认的Cell值
    default:
    cellvalue = " ";
    }
    } else {
    cellvalue = "";
    }
    // 在每个字段之间插入分割符
    str[i] = cellvalue;
    } return str;
    }}
      

  3.   

    都没看清楚问题
    是jxls  不是jxl