首先,我要感谢你们所有的时间阅读我的帖子!
我使用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;
}
}
或在.txt文件中打印结果。谢谢!
答案 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();