【问题标题】:How to read specific lines from excel in Java如何从 Java 中的 excel 中读取特定行
【发布时间】:2015-07-16 14:36:58
【问题描述】:

此处为 Excel 文件: http://s000.tinyupload.com/index.php?file_id=48668827125938494328

如果我想列出所有的高点,我该怎么做? 程序的输出应如下所示: 320 323 320 321 324

【问题讨论】:

  • 您使用哪种技术从 java 访问 excel?请在此处显示文件的内容,而不是指向潜在危险文件的可疑链接!

标签: java excel file writing


【解决方案1】:

将 excel 文件导出为 csv 格式,然后使用以下代码:

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class Main {

public static void main(String[] args) {

Path wiki_path = Paths.get(path, "TestSheet.csv");

Charset charset = Charset.forName("ISO-8859-1");
try {
  List<String> lines = Files.readAllLines(wiki_path, charset);
  int i = 0;
  for (String line : lines) {
    if (i>8) {
        String temp = line.substring(line.indexOf(',',line.indexOf(',')+1)+1);
        temp = temp.substring(0,temp.indexOf(','));
        System.out.println(temp);
    }
    i++;
  }
} catch (IOException e) {
  System.out.println(e);
}

}
}

不要忘记添加文件路径

【讨论】:

    【解决方案2】:

    抛开程序实现,使用Apache POI库,用Java干净地读写xls、xlsx文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多