我想创建一个简单的 java 代码,它可以读取 ODS 文件并将其单元格的内容存储为文本字符串。就这样。我对此很陌生,所以我陷入了基本错误。
我安装了 Eclipse,我下载了 Apache POI。我想我成功地将它添加到我的 Eclipse 项目中(至少我可以在项目资源管理器中看到我的项目中的 .jar 文件。
我在这里找到了这段代码:
import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFComment; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
try { POIFSFileSystem fs; fs = new POIFSFileSystem(new FileInputStream(C:\TableauConjugaison.ods));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFSheet sheet2 = wb.getSheetAt(1);
HSSFRow row;
HSSFCell cell;
int rows; // No of rows
rows = sheet.getPhysicalNumberOfRows();
int cols = 0; // No of columns
int tmp = 0;
// This trick ensures that we get the data properly even if it doesn't start from first few rows
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) {
// Your code here
}
}
}
} } catch(Exception ioe) {
ioe.printStackTrace();
}
所以我试着把它放在 eclipse 中,我点击了 Run 按钮,选择 node.js 应用程序(不是服务器的东西),我得到以下错误:
C:\Users\Olivier\workspace\Translation\WebContent\ExtractODS.js:3
POIFSFileSystem fs;
^^
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
我很确定我在这里遗漏了很多基本的东西,但我完全迷失了。有人可以指导我完成此操作的基本步骤吗?我花了 6 个小时寻找解决方案,但我所缺少的东西从未得到解释。
提前致谢。