【问题标题】:Once I read a excel file with apache POI how do I print the sheet in a txt file?使用 apache POI 读取 excel 文件后,如何在 txt 文件中打印工作表?
【发布时间】:2014-12-08 05:36:53
【问题描述】:

首先我要感谢大家花时间阅读我的帖子!

我使用 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 文件中打印结果。谢谢!

【问题讨论】:

  • 你有没有抛出异常?它打印任何东西吗?
  • 是的,它读取每一行并逐行打印,而不是矩阵。运行:xlsx=Family Data ID NAME LASTNAME AGE 1.0 Ingacio Alarcon 59 2.0 Esperanza Marambio 50 3.0 Juan Ignacio Alarcon 27 4.0 Katherine Marsh 25 BUILD SUCCESSFUL(总时间:2 秒)

标签: java excel netbeans


【解决方案1】:

我认为你的问题是你使用单元格迭代器,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
}

【讨论】:

  • 感谢@dkatzel,但我不明白我必须在哪里附加该代码。
  • 谢谢,但仍然打印行而不是矩阵。您还知道如何将读数导出到相同代码中的 txt 文件中吗?
【解决方案2】:
String actualOutputWBString = new XSSFExcelExtractor(actualWorkbook).getText();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 2013-05-18
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多