一旦我用apache POI读取excel文件,如何在txt文件中打印工作表?

时间:2014-10-13 15:32:35

标签: java excel netbeans

首先,我要感谢你们所有的时间阅读我的帖子!

我使用netbeans IDE 8.0.1,我是JAVA中的新手。

我一直在谷歌搜索多种方式阅读&使用Apache POI编写excel文件。有很多关于它的教程和信息,但我仍然无法编写我在.txt文件中读取的excel文件(或工作表)。

我有这个代码非常好,因为如果是xls或xlsx文件,则声明工作簿。但它不打印在netbeans中作为表格读取的内容,所以我认为如果我尝试将其打印为.txt文件,我将遇到同样的问题。

是的,有人能帮帮我吗?谢谢,抱歉代码粘贴是我的第一篇文章!

以下是代码:

package read_write_excel_V2;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class JIA_ReadingRewritingWorbooks_v2 { 
public static void main(String[] args) throws IOException { 
    String fname = "ExcelJAVAWrite_demo.xlsx"; 
    InputStream inp = new FileInputStream(fname); 
    String fileExtn = GetFileExtension(fname); 
    Workbook wb_xssf;       // Declare XSSF WorkBook 
    Workbook wb_hssf;       // Declare HSSF WorkBook 
    Sheet sheet = null;     

    if (fileExtn.equalsIgnoreCase("xlsx")) { 
        wb_xssf = new XSSFWorkbook(inp); 
        log("xlsx="+wb_xssf.getSheetName(0)); 
        sheet = wb_xssf.getSheetAt(0); 
    } 

    if (fileExtn.equalsIgnoreCase("xls")) { 
        POIFSFileSystem fs = new POIFSFileSystem(inp); 
        wb_hssf = new HSSFWorkbook(fs); 
        log("xls="+wb_hssf.getSheetName(0)); 
        sheet = wb_hssf.getSheetAt(0); 
    } 

    Iterator rows = sheet.rowIterator();

    while (rows.hasNext()) {
        Row row = (Row) rows.next();  

        Iterator cells = row.cellIterator(); 

        while (cells.hasNext()){ 
            Cell cell = (Cell) cells.next(); 

        switch ( cell.getCellType() ) {
            case Cell.CELL_TYPE_STRING: 
            log(cell.getRichStringCellValue().getString()); 
            break; 
            case Cell.CELL_TYPE_NUMERIC: 

                if(DateUtil.isCellDateFormatted(cell)) { 
                    log(cell.getDateCellValue()+""); 
                } else { 
                System.out.println(cell.getNumericCellValue()); 
                } 
                break; 
            case Cell.CELL_TYPE_BOOLEAN: 
                log(cell.getBooleanCellValue()+""); 
                break;
            case Cell.CELL_TYPE_FORMULA: 
                log(cell.getCellFormula()); 
                break; 
            default: 
        } 
        } 
    } 
    inp.close(); 
    } 

private static void log(String message) { 
    System.out.println(message); 
 } 

private static String GetFileExtension(String fname2) { 
    String fileName = fname2; 
    String fname=""; 
    String ext=""; 
    int mid= fileName.lastIndexOf("."); 
    fname=fileName.substring(0,mid); 
    ext=fileName.substring(mid+1,fileName.length()); 
    return ext; 
}
 }

我添加@dkatzel推荐的内容,但打印仍然是行。代码:

public class JIA_ReadingRewritingWorbooks_v2_Tmp { 
public static void main(String[] args) throws IOException { 
    ....

    Iterator rows = sheet.rowIterator(); 

    while (rows.hasNext()) {
        Row row = (Row) rows.next(); 

        int numCells =row.getLastCellNum();
        for(int i=0; i<numCells; i++){
        Cell cell = row.getCell(i);
            // If not defined, cell could be null
            // Do your cell printing here
            //Cell cell = (Cell) cells.next(); 

        switch ( cell.getCellType() ) {
            case Cell.CELL_TYPE_STRING: 
            log(cell.getRichStringCellValue().getString()); 
            break; 
            case Cell.CELL_TYPE_NUMERIC: 

                if(DateUtil.isCellDateFormatted(cell)) { 
                    log(cell.getDateCellValue()+""); 
                } else { 
                System.out.println(cell.getNumericCellValue()); 
                } 
                break; 
            case Cell.CELL_TYPE_BOOLEAN: 
                log(cell.getBooleanCellValue()+""); 
                break;
            case Cell.CELL_TYPE_FORMULA: 
                log(cell.getCellFormula()); 
                break; 
            default: 
        } 
        //}
        }

    } 
    inp.close(); 
} 

private static void log(String message) { 
    System.out.println(message); 
 } 

private static String GetFileExtension(String fname2) { 
    String fileName = fname2; 
    String fname=""; 
    String ext=""; 
    int mid= fileName.lastIndexOf("."); 
    fname=fileName.substring(0,mid); 
    ext=fileName.substring(mid+1,fileName.length()); 
    return ext; 
}
}

我有这样的结果: 这种结果我得到: https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/10527311_10153011702894311_4509818841257258400_n.jpg?oh=d724cbfc9a96aa29a07f0d26a7c81726&oe=54B5C2A5&gda=1421766835_79c0648ddbeb2eb966027cd62f8d241a

我希望得到这种类型的结果,表格如下: https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/10710855_10153011703104311_5065636883843539807_n.jpg?oh=9bc931559b3e2380068b7a7ecac35b57&oe=54ABCEC3&gda=1421306774_8cf6356575f6c255c52f316ed9d1fba0

或在.txt文件中打印结果。谢谢!

2 个答案:

答案 0 :(得分:0)

我认为你的问题是你使用了Javadoc所说的

的单元迭代器
  

在物理定义的单元格上单元格迭代器

我认为这意味着它会跳过空格。

如果要包含空格,则需要手动迭代它们:

而不是

Iterator cells = row.cellIterator(); 
while (cells.hasNext()){ 
        Cell cell = (Cell) cells.next(); 
       ...
}

这样做:

int numCells =row.getLastCellNum();
for(int i=0; i<numCells; i++){
    Cell cell = row.getCell(i);
    //if not defined, cell could be null
    //do your cell printing here
}

答案 1 :(得分:0)

String actualOutputWBString = new XSSFExcelExtractor(actualWorkbook).getText();