阅读空白excel细胞

时间:2014-04-30 07:17:52

标签: java apache-poi

我正在尝试阅读一个excel文件..但它也包含一些空白单元格。enter image description here

1http://i.stack.imgur.com/Tehfm.png我想将这些行读作1234567890,aaaaaaaa :1234567891,aaaaaaaa,aaaaaaaa:1234567892,aaaaaaaa,aaaaaaaaa .....意味着我想用逗号分隔单元格和冒号行,如果有一个空白单元格,那么我想在那里添加一个空格...我想知道这个代码但它不起作用enter image description here

1 个答案:

答案 0 :(得分:1)

使用网页http://jexcelapi.sourceforge.net/

中的Java Excel库

我已经使用示例文件测试了代码并且工作正常。

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.getRows(); j++)
            {
                for (int i = 0 ; i < sheet.getColumns(); i++)
                {
                    Cell cell = sheet.getCell(i,j);
                    CellType type = cell.getType();


                    if (type == CellType.LABEL)
                    {
                        System.out.print(cell.getContents() + ", ");
                    }
                    if (type == CellType.NUMBER)
                    {
                        System.out.print(cell.getContents() + ", ");
                    }
                    if (type == CellType.EMPTY)   
                    {
                        System.out.print(", ");
                    }

                }
            }

        }
        catch (BiffException e)
        {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException
    {
        ReadExcel test = new ReadExcel();
        test.setInputFile("c:/temporary/lars.xls");
        test.read();

    }
}