[英]Once I read a excel file with apache POI how do I print the sheet in a txt file?
首先,我要感謝您所有的時間閱讀我的帖子!
我使用netbeans IDE 8.0.1,並且在JAVA中是新手。
我一直在Google搜索使用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-aa.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/10527311_10153011702894311_4509818841257258400_n.jpg ? oh = d724cbfc9a96aa29a07f0d26a7c81726 54B5C2A5& gda = 1421766835_79c0648ddbeb2eb966027cd62f8d241a
或將結果打印為.txt文件。 謝謝!
我認為您的問題是您使用了單元迭代器, 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
}
String actualOutputWBString = new XSSFExcelExtractor(actualWorkbook).getText();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.