This example shares the specific code of Java using poi to operate Excel for your reference. The specific content is as follows
The jar package that depends on poi, the pom.xml configuration is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>excelDemo1</groupId> <artifactId>excelDemo1</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>excelDemo1 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.8</version> </dependency> </dependencies> <build> <finalName>excelDemo1</finalName> </build> </project>
The corresponding java test codes are as follows:
package excelDemo1; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ExcelDemo0 { /** * java generates excel file and writes it to disk* * @author: tuzongxun * @Title: main * @param@param args * @return void * @date Apr 28,2016 7:32:52 PM * @throws */ public static void main(String[] args) { //C:/Users/tuzongxun123/Desktop desktop, the slashes of windows and linux are different, and Java requires "/" to escape processing. File.separator can implement cross-platform File file = new File("C:" + File.separator + "Users" + File.separator + "tuzongxun123" + File.separator + "Desktop" + File.separator + "ioFile" + File.separator + "user.xls"); try { OutputStream outputStream = new FileOutputStream(file); // Create an excel file, note that the hssf here is available for excel2007 and previous versions, and is not available after 2007. Use xssf HSSFWorkbook workbook = new HSSFWorkbook(); // Create an excel worksheet HSSFSheet sheet = workbook.createSheet("user"); // Add a row to the worksheet HSSFRow row = sheet.createRow(0); // Add two cells to the specified row row.createCell(0).setCellValue("name"); row.createCell(1).setCellValue("password"); // Call the output stream to write the excel file to disk workbook.write(outputStream); // Close the output stream outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } package excelDemo1; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; 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.poifs.filesystem.POIFSFileSystem; /** * Read excel file* * @author tuzongxun123 * */ public class ExcelDemo2 { public static void main(String[] agrs) { try { // Get the excel file input stream FileInputStream fileInputStream = new FileInputStream("C:" + File.separator + "Users" + File.separator + "tuzongxun123" + File.separator + "Desktop" + File.separator + "ioFile" + File.separator + "user.xls"); BufferedInputStream bufferedInputStream = newBufferedInputStream( fileInputStream); POIFSFileSystem fileSystem = new POIFSFileSystem(bufferedInputStream); // Get the excel file HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem); // Get the specified excel worksheet based on the name HSSFSheet sheet = hssfWorkbook.getSheet("user"); // In fact, you can use sheet.rowIterator() to traverse for (int i = 1;; i++) { HSSFRow row = sheet.getRow(i); if (row != null) { String nameString1 = row.getCell(0).getStringCellValue(); String password = row.getCell(i).getStringCellValue(); System.out.println("name:" + nameString1); System.out.println("password:" + password); bufferedInputStream.close(); } else { bufferedInputStream.close(); return; } } } catch (Exception e) { e.printStackTrace(); } } }The above is all about this article, I hope it will be helpful for everyone to learn Java programming.