我是Excel - Java (Apache POI)的新手,正在尝试找出一种将excel数据转换为文本文件的方法。
我的excel表格如下所示
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*
}
}
}
}
}任何帮助都是非常感谢的。
发布于 2017-08-25 00:12:37
据我所知,在单元格上有一个getStringValue()方法。可能在读取字符串值之前,您已经将cellformat设置为string格式。读取值后,可以将行和单元格值存储在List < List < String> >中。(值)P,那么你应该找到最长的字符串长度。(k)
在for循环中为每个值调用一个for循环,并为字符串添加额外的空间,使其长度达到k。有一些编写器类,您可以在其中定义何时在文本文件中开始新行。
https://stackoverflow.com/questions/45866022
复制相似问题