简体   繁体   English

是否可以在同一xls表上进行读写?

[英]Is this possible to read and write on same xls sheet?

我正在使用JXl进行xls读取,因此可以在同一个Excel工作表中进行读写,我在硒项目中使用我的测试数据在读取我要在该Excel工作表中写入的每一行后都在xls工作表中。

You can not read & write same file in parallel( Read-write lock ). 您不能并行读写同一文件( 读写锁定 )。 But, we can do parallel operations on temporary data(ie Input/output stream). 但是,我们可以对临时数据(即输入/输出流)执行并行操作。 Write the data to file only after closing the input stream. 仅在关闭输入流后才将数据写入文件。 Below steps should be followed. 请遵循以下步骤。

  • Open the file to Input stream 打开文件输入流
  • Open the same file to an Output Stream 将同一文件打开到输出流
  • Read and do the processing 阅读并进行处理
  • Write contents to output stream. 将内容写入输出流。
  • Close the read/input stream, close file 关闭读/输入流,关闭文件
  • Close output stream, close file. 关闭输出流,关闭文件。

Sample code: 样例代码:

File inputFile = new File("D://"+file_name);
File outputFile = new File("D://"+file_name);
Workbook readCopy = Workbook.getWorkbook(inputFile);
WritableWorkbook writeCopy = Workbook.createWorkbook(outputFile,readCopy);

// instructions to put content in specific rows, specific columns

readCopy.close();    
inputFile.close();
writeCopy.write();
writeCopy.close();
outputFile.close();

Apache POI - read/write same excel example Apache POI-读/写相同的Excel示例

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class XLSXReaderWriter {

    public static void main(String[] args) {

        try {
            File excel = new File("D://raju.xlsx");
            FileInputStream fis = new FileInputStream(excel);
            XSSFWorkbook book = new XSSFWorkbook(fis);
            XSSFSheet sheet = book.getSheetAt(0);

            Iterator<Row> itr = sheet.iterator();

            // Iterating over Excel file in Java
            while (itr.hasNext()) {
                Row row = itr.next();

                // Iterating over each column of Excel file
                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");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;
                    default:

                    }
                }
                System.out.println("");
            }

            // writing data into XLSX file
            Map<String, Object[]> newData = new HashMap<String, Object[]>();
            newData.put("1", new Object[] { 1d, "Raju", "75K", "dev",
                    "SGD" });
            newData.put("2", new Object[] { 2d, "Ramesh", "58K", "test",
                    "USD" });
            newData.put("3", new Object[] { 3d, "Ravi", "90K", "PMO",
                    "INR" });

            Set<String> newRows = newData.keySet();
            int rownum = sheet.getLastRowNum();

            for (String key : newRows) {
                Row row = sheet.createRow(rownum++);
                Object[] objArr = newData.get(key);
                int cellnum = 0;
                for (Object obj : objArr) {
                    Cell cell = row.createCell(cellnum++);
                    if (obj instanceof String) {
                        cell.setCellValue((String) obj);
                    } else if (obj instanceof Boolean) {
                        cell.setCellValue((Boolean) obj);
                    } else if (obj instanceof Date) {
                        cell.setCellValue((Date) obj);
                    } else if (obj instanceof Double) {
                        cell.setCellValue((Double) obj);
                    }
                }
            }

            // open an OutputStream to save written data into Excel file
            FileOutputStream os = new FileOutputStream(excel);
            book.write(os);
            System.out.println("Writing on Excel file Finished ...");

            // Close workbook, OutputStream and Excel file to prevent leak
            os.close();
            book.close();
            fis.close();

        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 excel 表读取数据并写入同一 excel 表 - Read data from excel sheet and write in the same excel sheet 是否可以同时读写文件? - Is it possible to read and write in file at the same time? 如何在同一Excel工作表中读取和写入测试用例的结果 - How to Read & write Result of Test Case in same Excel sheet 通过HSSF.EventUserModel读取带有受保护书籍和表格的XLS - Read XLS with Protected Book and Sheet via HSSF.EventUserModel 是否可以允许不同的线程同时读取和写入同一集合? - Is it possible to allow different thread both read to and write from the same collection? 使用TestNG DataProvider时,是否可以从SAME Excel工作表中读取和写入参数? - When using TestNG DataProvider is there a way to read and write parameters from the SAME Excel sheet? 如何从.txt转换为.xls文件读/写? - How do I convert from .txt to .xls file read/write? 无法使用 ApachePOI android studio 将连续数据写入/记录到 a.xls 工作表 - Unable to write/record continuous data to a .xls sheet using ApachePOI android studio 是否可以使用usermodel编写Excel文件并使用Apache POI中的eventmodel再次读取同一文件 - Is it possible to write an Excel file using usermodel and read the same file again using eventmodel in apache poi 是否可以创建一个文本文件,然后在代码的同一运行或运行时也读写这些文件? Java - Is it possible to create a text file and then on the same run or runtime of the code also read and write in these files? Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM