java 复制xls,将Excel数据复制到文本文件-Java(POI)

这篇博客讨论了如何使用Java和Apache POI库将Excel电子表格的内容转换并保存到文本文件中。作者面临的问题是如何读取Excel单元格的字符串值,并有效地存储到列表中。解决方案涉及到获取每个单元格的字符串值,然后存储每一行和单元格的值,并在文本文件中格式化输出。博客提到了处理不同长度字符串的方法,以便在输出时保持整齐的格式。
摘要由CSDN通过智能技术生成

I am new to Excel - Java (Apache POI) and trying to figure out a way to transfer excel data to a text file.

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

My Code so far

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*

}

}

}

}

}

Any help is appreciated.

解决方案

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)

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值