日期:2014-05-20  浏览次数:20695 次

jxl合并单元格,读该合并之后的单元格,读出数据出现与单元格数据不一致问题,请熟悉jxl的兄弟入(附代码)
Java code

package cn.com.zte.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * 读写excel文件
 * @author huangshaojun
 *
 */
public class TestExcel
{

    /**
     * 写Excel文件
     * @param fileName
     */
    public void writeExcel(String fileName)
    {
        OutputStream outStream = null;  //输出流
        WritableWorkbook writeWork = null;  //写工作薄
        try
        {
            outStream = new FileOutputStream(fileName); //文件输出流
            writeWork = Workbook.createWorkbook(outStream); //新建工作薄
            WritableSheet outSheet = writeWork.createSheet("old", 0);   //新建工作页
            int k = 1;
            for(int i = 0 ; i < 10; i++)
            {
                for(int j = 0 ; j < 10 ; j++)
                {
                    Label lable = new Label(i , j , String.valueOf(k++)); //新建单元格
                    outSheet.addCell(lable);    //把单元格添加到工作页中
                }
            }
            outSheet.mergeCells(1, 1, 1, 5);    //合并单元格,参数格式(开始列,开始行,结束列,结束行)
            writeWork.write();
            writeWork.close();  //关闭
            outStream.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
    }
    
    /**
     * 读Excel文件
     * @param fileName
     */
    public void readExcel(String fileName)
    {
        InputStream inStream = null;
        Workbook workBook = null;
        try
        {
            inStream = new FileInputStream(fileName);   //输入流
            workBook = Workbook.getWorkbook(inStream);  //工作薄
            Sheet inSheet = workBook.getSheet("old");   //sheet页
            
            int columns = inSheet.getColumns();
            int rows = inSheet.getRows();
            
            StringBuffer CellContent = new StringBuffer();
            for(int i = 0; i < columns; i++)
            {
                for(int j = 0 ; j < rows ; j++)
                {
                    CellContent.append(inSheet.getCell(i, j).getContents());    //读单元格内容
                    CellContent.append(" ");
                }
                System.out.println(CellContent);
                CellContent.delete(0, CellContent.length());
            }
                        
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        TestExcel te = new TestExcel();
        te.writeExcel("D:\\old.xls");
        te.readExcel("D:\\old.xls");
    }

}





jxl合并单元格,读该合并之后的单元格,读出数据出现与单元格数据不一致问题,

读出结果:
1 2 3 4 5 6 7 8 9 10 
11 12 13 14 15 16 17 18 19 20 
21 22 23 24 25 26 27 28 29 30 
31 32 33 34 35 36 37 38 39 40 
41 42 43 44 45 46 47 48 49 50 
51 52 53 54 55 56 57 58 59 60 
61 62 63 64 65 66 67 68 69 70 
71 72 73 74 75 76 77 78 79 80 
81 82 83 84 85 86 87 88 89 90 
91 92 93 94 95 96 97 98 99 100 

原本是13 14 15 16 这几个数据是没有的,合并到12单元格里面了,我打开这个excel文件,也的确是没有这几个数据,但程序读就读多了这几个数据,请高手指点。。。

------解决方案--------------------
我只会普通的导入导出
你的高级货,没有研究过
帮你顶下