将 Excel 数据复制到文本文件 - Java (POI) [英] Copying Excel data to Text File - Java (POI)
问题描述
我是 Excel 新手 - Java (Apache POI) 并试图找出一种将 excel 数据传输到文本文件的方法.
I am new to Excel - Java (Apache POI) and trying to figure out a way to transfer excel data to a text file.
我的excel表是这样的
My excel sheet looks something like this
name table latest_table Date
1 george table1 t459 2017-08-24
2 john table1 thi7 2017-07-23
3 mary table1 gdr99 2017-07-22
到目前为止我的代码
public static void excel_to_text(XSSFWorkbook wb) {
XSSFSheet sheet = wb.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
int cols = 0, tmp = 0;
XSSFRow row;
XSSFCell cell;
for(int i = 0; i < 10 || i < rows; i++) {
row = sheet.getRow(i);
if(row != null) {
tmp = sheet.getRow(i).getPhysicalNumberOfCells();
if(tmp > cols) cols = tmp;
}
}
for(int r = 0; r < rows; r++) {
row = sheet.getRow(r);
if(row != null) {
for(int c = 0; c < cols; c++) {
cell = row.getCell((short)c);
if(cell != null) {
*// code here*
}
}
}
}
}
感谢任何帮助.
推荐答案
据我所知,单元格上有一个 getStringValue() 方法.也许您在读取字符串值之前已将 cellformat 设置为字符串格式.读取值后,您可以将行和单元格值存储在 List <列表<字符串> >.(值)磷比你应该找到最长的字符串长度.(k)
As far as I remember there is a getStringValue() method on cells. Maybe you have set the cellformat to string format before reading the string value. After reading the value you can store the rows and cell values in a List < List < String> >. (values) P Than you should find the longest string length. (k)
在 for 循环中为每个值调用一个 for 循环,并为字符串添加额外的空间以达到 k 长度.有一些编写器类,您可以在其中定义何时开始文本文件中的新行.
Call a for cycle inside a for cycle for every value and add extra space for the strings to reach k length. There are writer classes where you can define when to start a new row in the text file.
这篇关于将 Excel 数据复制到文本文件 - Java (POI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!