简体   繁体   中英

Java code to access data from an excel sheet and write that specific data to another excel sheet

I need to read data from a row of excel sheet through Java code and writing the content of it to another excel sheet but not in the same column but in different columns. Can anyone help me ? THANKS a lot in advance :)

Example: Suppose from the first excel file , I got this from one of the column : CN=user1,CN=Users,DC=example,DC=com

Now, I need to put this data in another excel sheet but in different columns ie each of the comma separated values will go to different columns.

You could try exporting to tab-delineated text document and manipulating that using Java code.

https://www.howtogeek.com/79991/convert-an-excel-spreadsheet-to-a-tab-delimited-text-file/

Just use Java's I/O capabilities from there.

https://docs.oracle.com/javase/tutorial/essential/io/

I've recently used the Apache POI library for parsing excel spreadsheets and found it incredibly useful.

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

void parseWorkbook(File file) {
    POIFSFileSystem fs = new POIFSFileSystem(file.getInputStream());
    HSSFWorkbook wb = new HSSFWorkbook(fs);
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            parseSheet(wb.getSheetAt(i));
        }
}

void parseSheet(HSSFSheet sheet) throws IllegalStateException {
    final int rows = sheet.getPhysicalNumberOfRows();

    HSSFRow row;
    for (int r = 0; r < rows; r++) {
        row = sheet.getRow(r);
        if (row != null) {
            parseRow(row);
        }
    }
 }

 void parseRow(HSSFRow row) {
    row.getCell(0);
    ....
 }

An example of reading and writing to a spreadsheet can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM