- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
从给定的 .xlsx
文件,我尝试使用 Java 读取数据。
我读取文件的代码如下:
public static void main(String[] args) throws Exception {
FileInputStream file = new FileInputStream(new File("E:\\test1.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet test = workbook.getSheetAt(0);
student emp = new student();
Iterator<Row> itr = test.iterator();
itr.next();
while(itr.hasNext()){
Row row = itr.next();
emp.reedData(row);
System.out.println(id+","+name+","+options);
}
方法如下:
void reedData(Row row){
id= row.getCell(0).toString();
name= row.getCell(1).toString();
options= row.getCell(2).toString();
}
但是,我得到这样的输出:
1.0,X,play game
,,sing song
2.0,Y,play game
,,sing song
我希望输出看起来像这样,而不是上面的:
1.0,X,{play game,sing song}
2.0,Y,{play game,sing song}
这个问题是因为我正在合并 .xlsx
文件中的两个单元格。
有什么建议吗?提前谢谢你..
最佳答案
我是这样做的。
import static java.lang.System.out;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class read {
public static void main(String [] args){
FileInputStream excelFile = new FileInputStream(new File(keep));
XSSFWorkbook wb = new XSSFWorkbook(excelFile);
XSSFSheet sheet = wb.getSheetAt(0);
int rowCount; // number of rows of newly form excel files
rowCount= sheet.getPhysicalNumberOfRows();
out.println(rowCount);
//trying to iterate the excel file
int i =0;
Iterator<Row> rowIterator = sheet.iterator();
do{
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell.getColumnIndex()==0) { // to read the first column
DataFormatter df = new DataFormatter();
String str2 = df.formatCellValue(cell);
customerNo.add(str2);
}
}
++i;
if(i>rowCount)break;
//break;
}while(rowIterator.hasNext());
wb.close();
}
}
希望这对您有所帮助。
关于java - 如何在 java 中读取 Excel (.xlsx) 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37811334/
引用这个例子https://github.com/SheetJS/js-xlsx/blob/master/tests/write.js ,它在 xlsx 电子表格中没有任何单元格公式的实现我尝试使用
我在我的 angular2 应用程序中使用 xlsx-style npm 模块来动态创建 Excel。使用 xlsx-style 而不是 xlsx 模块的原因是因为我需要设置单元格的样式,而使用 xl
我正在尝试将类似 json 的数据下载为 xlsx。我用过xlsx npm 包并在线遵循了一些示例代码,但是当我尝试在 Excel 中打开该文件时,出现此错误: /* original data */
假设我有一个 excel 文件,我想使用 read.xlsx 函数将其读取到 R 中。文件由电子表格组成,我不知道电子表格的数量(大约有 200 个这样的文件,因此手动检查表格数量会很痛苦)。每个电子
我有一个包含多个工作表的 .xlsx 文件,我想将所有工作表拆分为不同的 .xlsx 文件。例如,我有一个文件 matt.xlsx,它有 5 个工作表,名称分别为 A、B、C、D、E。我想将它们分成
在我的 angular 5 应用程序中,我使用 xlsx 从 microsoft excel 文件中读取内容(老师的联系信息,如电子邮件和姓名),在 webpack 应用程序用于生产后,我发现 xls
我正在尝试使用 xlsx 将带有日期列的数据框导出到 Excel包裹。 write.xlsx() 的帮助文件说明了格式化日期列的方法。我尝试按照下面的方法进行操作,但生成的 Excel 文件没有显示正
我正在尝试使用 write.xslx 保存文件(使用 write.csv 保存时,某些行在更多列中移动,因此我正在尝试保存文件直接作为xlsx)。如果我输入这个命令: write.xlsx (
我需要在 nodejs 应用程序中编辑 XLSX 文件以创建订单表。我需要编辑的文件有特定的格式和一些公式。我尝试使用多个库,但我发现唯一一个能够读写 xlsx 文件的库是 XLSX.js . 但是当
我正在尝试使用 js-xlsx 为导出的 excel 文件设置固定的列/单元格宽度。 编辑: 这里是js-xlsx的来源:https://github.com/SheetJS/js-xlsx 最佳答案
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 8 个月前关闭。 Improve this
我有一个大的 .xlsx 文件,我想根据第一列将其拆分为多个文件。数据结构有点不确定,这使它复杂化了很多,而且我是 python 的新手。 基本上我需要为以“Brand1”(见下文)开头的每一行创建一
我正在尝试从一个大文件(800k 行)中获取数据并通过 lambda (AWS) 将其放入数据库中。为此,我从 S3 获取 xlsx 文件作为缓冲区并读取它。 module.exports.getSa
我对 xlsx R 包的 write.xlsx2 函数有问题。例如,请参阅下面的代码。 main_path<-"~/mydir/" read.xlsx2(paste0(main_path,"my_in
使用 R 的 xlsx 包创建 XLSX 文件时,默认情况下,带有字符串的列默认向左对齐,带有整数的列向右对齐(混合了整数和字符串的列也向左对齐)。最终,我想通过将所有列都向左对齐来标准化所有列,但是
在写入文件时找不到设置单个单元格样式的方法。不过看书没问题。尝试过 js-xlsx、xlsx-style,似乎都不行?还是我读错了 API? 如果我没看错的话,将 cell.s 设置为这样的对象就足够
我正在使用包 xlsx 版本:0.5.7 日期:2014-08-01。在 R 版本 3.0.1 (2013-05-16) 中——“Good Sport”平台:i386-w64-mingw32/i386
我正在使用此函数对数据进行排序并将数据导出到 Excel window.exportData = function () { data.sort(function(a, b){
如何使用 Node js 中的 sheetjs 的 xlsx 包在已有的 xlsx 文件中创建/添加新的工作表文件? 这是我迄今为止针对现有“todo-list.xlsx”文件的代码。 const x
我正在尝试复制 Excel 文档,但它似乎只复制第一个工作表,而不复制原始文件中的工作表名称。 我尝试复制它,当失败时,我查找了如何执行此操作,但是,问题是我不知道使用前的工作表名称是什么,也不知道有
我是一名优秀的程序员,十分优秀!