Java implements reading excel and modifying part of the content and finally writing it to a new file
pom.xml introduces related jxl.jar and poi.jar
pom file is as follows
<dependencies> <!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl --> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId> jxl </artifactId> <version>2.6.12</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> </dependencies>
The specific code of java implementation is as follows:
package Util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFComment; 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.util.CellAddress; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableCell; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExcelHandler { /** * jxl read file *workbook is a read-only file that can read the content * @throws BiffException * @throws IOException */ public static void ExcelReader(){ File file=new File("C:\\Users\\Administrator\\Desktop\\excels\\example.xls"); Workbook workbook; try { workbook = Workbook.getWorkbook(file); Sheet sheet=workbook.getSheet(0); Cell cell=sheet.getCell(2, 1); Cell cell1=sheet.getCell(2, 1); Cell cell2=sheet.getCell(2, 1); Cell cell3=sheet.getCell(2, 1); System.out.println(cell.getContents()); } catch (BiffException | IOException e) { // TODO Auto-generated catch block e.printStackTrace (); } } /**‘ * jxl realizes excel read and output * Re-output after reading excel modification */ public static void ModifyExcelAndOutput() { try { File file=new File("C:\\Users\\Administrator\\Desktop\\excels\\example.xls"); //The workbook read here is used as a template Workbook workbook=Workbook.getWorkbook(file); //here is the workbook that will be output for(int i=0;i<2;i++){//This simulation outputs two files // output file name String outFileName="C:\\Users\\Administrator\\Desktop\\excels\\output"+i+".xls"; // The jxl.Workbook object is read-only, so if you want to modify Excel, you need to create a readable copy that points to the original Excel file (ie new File(excelpath) below) //WritableWorkbook will overwrite the original file if the directly createWorkbook template file WritableWorkbook writeBook=Workbook.createWorkbook(new File(outFileName),workbook); //read the first sheet WritableSheet sheet=writeBook.getSheet(0); //Read the cell to be modified WritableCell cell=sheet.getWritableCell(2, 1); //Get the format of the previous cell jxl.format.CellFormat cf = cell.getCellFormat (); Label lable=new Label(2, 1, "Business Name: Modified Business Name"+i); //Set the format of the modified cell to the same as the original lable.setCellFormat(cf); // Put the modified cell back into the sheet sheet.addCell(lable); writeBook.write(); writeBook.close(); } workbook.close(); } catch (Exception e) { e.printStackTrace (); } } /** * Poi realizes excel modification * Read excel and modify some content and output */ public static void ModifyAndExport() { InputStream io; try { io = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\excels\\output0.xls")); HSSFWorkbook workbook = new HSSFWorkbook(io); HSSFSheet sheet=workbook.getSheetAt(0); for(int i=2;i<5;i++){ HSSFRow row=sheet.getRow(4); HSSFCell cell=row.getCell(0); cell.setCellValue("Contact Name: Wang"+i+"Mazi"); HSSFCell cell1=row.getCell(3); cell1.setCellValue("手机:"+i+"10110"); String outputPath="C:\\Users\\Administrator\\Desktop\\excels\\output"+i+".xls"; FileOutputStream fo=new FileOutputStream(new File(outputPath)); workbook.write(fo); } workbook.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace (); } } public static void main(String[] args) { ModifyAndExport(); } }