Java to read and write Excel

 

 

At present, there are two common ways, jxl and POI. Because the more powerful POI, operational performance in the face of large amounts of data more than jxl, therefore, generally use POI at work to operate excel.

      POI is an open-source library under Apache, POI provides a series of api for java programmers to provide read and write capabilities of Microsoft Office document formats.

 

The film will use

Example POI mode:

First of all tools, read and write Excel classes, Excelutil1

 

 The following code, the code basically copied from the Internet, but debugging a bit, so that he can use:

 

package com.cailian.test.framework.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/ **
*
*
* @author
* @version 2019 Nian 5 Yue 7 Ri
* /
public class ExcelUtill1 {

public static void setExcleFile (String filePath, rowNumber int, int colNumber, the Result int, String sheetName) throws Exception {
FileInputStream ExcleFile;
System .out.println (333);


the try {
// the FileInputStream file object instantiation Excle;
ExcleFile new new = the FileInputStream (filePath);
// XSSFWorkbook object instantiation Excle file;


XSSFWorkbook ExcelWBook = new new XSSFWorkbook (ExcleFile);
/ *
* examples of XSSFSheet objects, the sheet name designated ExcelFile, for subsequent operation of the sheet rows and columns;
*
* /
ExcelWSheet = ExcelWBook.getSheet XSSFSheet (sheetName);
// function parameter specified by the cell row and column, obtaining the object of a specified cell;
// = ExcelWSheet.getRow the Cell (Row) .getCell (COL);
XSSFRow Row = ExcelWSheet.getRow (rowNumber);
XSSFCell row.getCell COL = (colNumber);

/ *
* 1. If the cell type is a string type, use getStringCellValue (); to get the contents of the cell;
* 2. If the cell the type of numeric types, using getNumberricCellValue (); to get the contents of the cell;
* Note: getNumberricCellValue (); return value of type double, converted to a string, must be in the
() * getNumberricCellValue; preceded ( "") double-quotes for the cast to String type, not double quotes
* will throw error; double type can not be converted to a String type of anomaly;
*
* /
Col.getCellType CellData = String () == XSSFCell.CELL_TYPE_STRING col.getStringCellValue () + ""?
: String.valueOf (Math.round (col.getNumericCellValue ()));


// cells have content, you can directly the method of setting calls seCellValue cell values
col.setCellValue (result);

file output stream object // instantiate Excel file writing
a FileOutputStream FILEOUT new new = a FileOutputStream (filePath);
// write content in Excel
ExcelWBook.write ( FILEOUT);
fileOut.flush ();
fileOut.close ();
} the catch (Exception E) {
e.printStackTrace ();

}
}
static void setExcleFile1 public (String filePath, rowNumber int, int colNumber, Result String, String sheetName) throws Exception {
FileInputStream ExcleFile;
System.out.println (333);


the try {
// instantiate Excle FileInputStream file objects;
ExcleFile new new = the FileInputStream (filePath);
// instantiate objects Excle file XSSFWorkbook;


HSSFWorkbook ExcelWBook = new new HSSFWorkbook (ExcleFile);
/ *
* XSSFSheet instantiate objects, the sheet name designated ExcelFile, for subsequent operation of the sheet rows and columns;
*
* /
HSSFSheet ExcelWSheet = ExcelWBook.getSheetAt (0);
// the specified row and column of cells by a function parameter, acquisition of the object specified cell;
The Cell ExcelWSheet.getRow = // (Row) .getCell (COL);
HSSFRow Row = ExcelWSheet.getRow (rowNumber);
HSSFCell row.getCell COL = (colNumber);

/ *
* 1. If the cell type is a string type using getStringCellValue (); to get the contents of the cell;
* 2. If the cell type is a digital type using getNumberricCellValue (); to get the contents of the cell;
* Note: getNumberricCellValue (); return value of type double , converted to a string type, you must
() * getNumberricCellValue; preceded ( "") double-quotes for the cast to string type, not double quotes
* will throw error; double type can not be converted to a string exceptions;
*
* /
String = CellData col.getCellType () == XSSFCell.CELL_TYPE_STRING col.getStringCellValue () + ""?
: String.valueOf (Math.round (col.getNumericCellValue ()));


// the content in the cell, you can call the method of setting the value of the cell seCellValue directly
col.setCellValue (Result);

// write instantiated Excel file output file stream object
a FileOutputStream FILEOUT new new = a FileOutputStream (filePath);
// write content in Excel
ExcelWBook.write (FILEOUT);
fileOut.flush ();
fileOut.close ();



} the catch (Exception E) {
e.printStackTrace ();

}


}

// test that can be used, their use may be to test their files
// public static void main (String [] args) {
// the try {
// setExcleFile1 ( "D: / cailian_pg / test scripts / functional testing /pg3.0/src/test/resources/data/ui/d1value/cityDataBeijing.xls",
// 2, 2, "3", "Sheet" );
//} the catch (Exception E) {
// // Generated the catch-the TODO Auto Block
// e.printStackTrace ();
//}
// System.out.println (333);
//}

}

Step use tools for testing:

the UI test automation, a value obtained on the page should be written in Excel, the need to obtain this value, then call the method setExcleFile1 write Excel on it.
When calling the method parameters are: file name, number of rows, number of columns, the value written, sheet single name.

 

 Demand is: Excel to perform each of the embodiments, the need to write a value fixed by the columns in each row. For example, it is 30 in each row.

Line method can be used caseID, the column is fixed, the file name is written dead.

If the file name is also required to change, you can use the configuration file. The next article. . . . .


Step Three: When you run, you find that the value has been written in the document,





 

Guess you like

Origin www.cnblogs.com/yqcf/p/11615880.html