[英]Once I read a excel file with apache POI how do I print the sheet in a txt file?
First I would like to thank all for your time in reading my post! 首先,我要感谢您所有的时间阅读我的帖子!
I use netbeans IDE 8.0.1 and I´m new in JAVA. 我使用netbeans IDE 8.0.1,并且在JAVA中是新手。
I have been searching in google many ways to read & write excel files using Apache POI. 我一直在Google搜索使用Apache POI读写Excel文件的多种方式。 There are a lot of tutorials and information about it, but I still can´t write the excel file (or sheet) that I read in a .txt file.
有很多关于它的教程和信息,但是我仍然无法编写在.txt文件中读取的excel文件(或表格)。
I have this code that is really good because declare a workbooks if is a xls or a xlsx file. 我的代码非常好,因为如果是xls或xlsx文件,则声明一个工作簿。 But it dosen't print what is read as a table in netbeans, so I think that I will have the same problem if I try to print it as a .txt file.
但是它不能打印在netbeans中作为表读取的内容,因此,我认为如果尝试将其打印为.txt文件,也会遇到同样的问题。
Can someone help me please? 有人能帮助我吗? Thanks and sorry for the code paste is my first post!
谢谢,抱歉,代码粘贴是我的第一篇文章!
Here is the code: 这是代码:
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;
}
}
I add what @dkatzel recommends but the printing is still in rows. 我添加了@dkatzel建议的内容,但打印仍然成行。 The code:
编码:
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;
}
}
I have this results: This type of results I´m getting: https://fbcdn-sphotos-aa.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/10527311_10153011702894311_4509818841257258400_n.jpg?oh=d724cbfc9a96aa29a07f0d26a7c81726&oe=54B5C2A5& gda =1421766835_79c0648ddbeb2eb966027cd62f8d241a 我得到了以下结果:我得到的这种结果: https ://fbcdn-sphotos-aa.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/10527311_10153011702894311_4509818841257258400_n.jpg ? oh = d724cbfc9a96aa29a07f0d26a7c81726 54B5C2A5& gda = 1421766835_79c0648ddbeb2eb966027cd62f8d241a
I wish to have this type of results, tabulated: https://fbcdn-sphotos-ba.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/10710855_10153011703104311_5065636883843539807_n.jpg?oh=9bc931559b3e2380068b7a7ecac35b57&oe=54ABCEC3& gda =1421306774_8cf6356575f6c255c52f316ed9d1fba0 我希望将这种类型的结果制成表格: https: //fbcdn-sphotos-ba.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/10710855_10153011703104311_5065636883843539807_n.jpg ? oh = 9bc931559b3e2380068b7a7ecac35b57 & oe = 54ABC 1421306774_8cf6356575f6c255c52f316ed9d1fba0
or print the results in .txt file. 或将结果打印为.txt文件。 Thanks!
谢谢!
I think your problem is you use the cell iterator, which the Javadoc says 我认为您的问题是您使用了单元迭代器, Javadoc说
Cell iterator over the physically defined cells
物理定义的单元上的单元迭代器
Which I take to mean it will skip blank cells. 我的意思是它将跳过空白单元格。
If you want to include blanks you will need to iterate over them manually: 如果要包含空格,则需要手动对其进行迭代:
instead of 代替
Iterator cells = row.cellIterator();
while (cells.hasNext()){
Cell cell = (Cell) cells.next();
...
}
Do this: 做这个:
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.