-1

我的数据以一种格式存储(向下看):[-] 表示空白单元格,右侧可能只有 10 列,在空格之后。像这样的东西:

        [string0] [-] [string1] [string2] [string3] .. [string10] [-]

如何将此代码更改为:

  1. 获取完整字符串,对于每一行 fullString = [-] [string1] [string2] [string3] .. [string10] [-]。字符串生成器?或者怎么做?

    //Get first sheet from the workbook
    HSSFSheet sheet = workbook.getSheetAt(0);
    
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();
    while(rowIterator.hasNext()) {
        Row row = rowIterator.next();
    
        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while(cellIterator.hasNext()) {
    
            Cell cell = cellIterator.next();    
            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    list1.add(cell.getStringCellValue());
                    break;
            }
        }
        System.out.println("");
    }
    file.close();
    FileOutputStream out =
            new FileOutputStream("C:\\Users\\student3\\"+sfilename+".xls");
    workbook.write(out);
    out.close();
    
4

1 回答 1

0

外部链接繁忙的 HSSF 和 XSSF 功能开发人员指南

这是一个应该工作的例子。

Maven依赖:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>

代码:

import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

public class StackOverflowQuestion18095443 {

    public static void main(String[] args) {
        if(args.length != 1) {
            System.out.println("Please specify the file name as a parameter");
            System.exit(-1);
        }
        String sfilename = args[0];
        File file = new File("C:\\Users\\student3\\" + sfilename + ".xls");
        read(file);
    }

    public static void read(File file) {
        try (InputStream in = new FileInputStream(file)) {
            HSSFDataFormatter formatter = new HSSFDataFormatter();
            Workbook workbook = WorkbookFactory.create(in);
            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                StringBuilder rowText = new StringBuilder();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    String cellAsStringValue = formatter.formatCellValue(cell);
                    rowText.append(cellAsStringValue).append(" ");
                }
                System.out.println(rowText.toString().trim());
            }
        } catch (InvalidFormatException | IOException e) {
            e.printStackTrace();
        }
    }
}
于 2013-08-07T06:01:59.163 回答