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 is configured as follows:
<project xmlns="/POM/4.0.0"xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0http:///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></url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId></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 ; import ; import ; import ; import ; import ; 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 be escaped, which can be implemented cross-platform File file = new File("C:" + + "Users" + + "tuzongxun123" + + "Desktop" + + "ioFile" + + ""); try { OutputStream outputStream = new FileOutputStream(file); // Create an excel file. Note that the hssf here is available in Excel2007 and previous versions, and it is not available after 2007. Use xssf to HSSFWorkbook workbook = new HSSFWorkbook(); // Create an excel worksheet HSSFSheet sheet = ("user"); // Add a row to the worksheet HSSFRow row = (0); // Add two cells to the specified row (0).setCellValue("name"); (1).setCellValue("password"); // Call the output stream to write the excel file to disk (outputStream); // Turn off the output stream (); } catch (Exception e) { (); } } }
package excelDemo1; import ; import ; import ; import ; import ; import ; import ; /** * 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:" + + "Users" + + "tuzongxun123" + + "Desktop" + + "ioFile" + + ""); BufferedInputStream bufferedInputStream = newBufferedInputStream( fileInputStream); POIFSFileSystem fileSystem = new POIFSFileSystem( bufferedInputStream); // Get the excel file HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem); // Get the specified excel workbook according to the name HSSFSheet sheet = ("user"); // You can actually use () to traverse here for (int i = 1;; i++) { HSSFRow row = (i); if (row != null) { String nameString1 = (0).getStringCellValue(); String password = (i).getStringCellValue(); ("name:" + nameString1); ("password:" + password); (); } else { (); return; } } } catch (Exception e) { (); } } }
The above is all about this article, I hope it will be helpful for everyone to learn Java programming.