Home > Java > javaTutorial > body text

Using Java to develop the import and export functions of form data

PHPz
Release: 2023-08-08 14:25:48
Original
1199 people have browsed it

Using Java to develop the import and export functions of form data

Use Java to develop the import and export functions of form data

In the modern software development process, the import and export functions of form data are very common requirements. Whether it is importing existing data into the system or exporting data in the system to a specific format, developers need to use appropriate technology to implement these functions. This article will use Java language as an example to introduce how to use Java to develop the import and export functions of form data.

  1. Form data import

In the process of form data import, we need to read and parse the external data, and then store the data into the system. Common data formats include Excel, CSV, XML, etc. The following is a sample code that uses the Apache POI library to import Excel data and store it in the database:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class FormImportExample {
    public static void main(String[] args) {
        try {
            // 连接数据库
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB", "username", "password");

            // 打开Excel文件
            FileInputStream file = new FileInputStream(new File("data.xlsx"));
            
            // 创建工作簿
            Workbook workbook = new XSSFWorkbook(file);

            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);

            // 遍历每一行
            for (Row row : sheet) {
                // 读取每一列的数据
                String col1 = row.getCell(0).getStringCellValue();
                int col2 = (int) row.getCell(1).getNumericCellValue();
                String col3 = row.getCell(2).getStringCellValue();
                
                // 将数据存储到数据库
                String sql = "INSERT INTO myTable (col1, col2, col3) VALUES (?, ?, ?)";
                PreparedStatement stmt = conn.prepareStatement(sql);
                stmt.setString(1, col1);
                stmt.setInt(2, col2);
                stmt.setString(3, col3);
                stmt.executeUpdate();
                stmt.close();
            }

            // 关闭文件和数据库连接
            file.close();
            conn.close();
            
            System.out.println("数据导入成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In the above code, we use the Apache POI library to read the contents of the Excel file and insert the data through JDBC into the database.

  1. Form data export

In the process of form data export, we need to obtain data from the system and export the data in a specific format. Likewise, common formats include Excel, CSV, XML, etc. The following is a sample code that uses the Apache POI library to export data to an Excel file:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class FormExportExample {
    public static void main(String[] args) {
        try {
            // 连接数据库
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDB", "username", "password");

            // 获取数据
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM myTable");

            // 创建工作簿
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("Data");

            // 创建表头
            Row header = sheet.createRow(0);
            header.createCell(0).setCellValue("Col1");
            header.createCell(1).setCellValue("Col2");
            header.createCell(2).setCellValue("Col3");

            // 填充数据
            int rowIndex = 1;
            while (rs.next()) {
                Row row = sheet.createRow(rowIndex++);
                row.createCell(0).setCellValue(rs.getString("col1"));
                row.createCell(1).setCellValue(rs.getInt("col2"));
                row.createCell(2).setCellValue(rs.getString("col3"));
            }

            // 导出文件
            FileOutputStream file = new FileOutputStream("exported_data.xlsx");
            workbook.write(file);
            
            // 关闭文件和数据库连接
            file.close();
            stmt.close();
            conn.close();
            
            System.out.println("数据导出成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Copy after login

In the above code, we get the data from the database and use the Apache POI library to create an Excel file and populate the data into the work table.

Through the above sample code, we can see that using Java to develop the import and export functions of form data can operate various data formats very conveniently. Of course, in addition to the Apache POI library, there are other open source libraries that can implement similar functions, such as OpenCSV, JExcel, etc. Developers can choose the library that suits them according to their specific needs to implement related functions.

In summary, whether it is importing or exporting form data, Java provides a wealth of tools and libraries to implement these functions. Developers only need to select appropriate technologies and libraries based on specific needs to easily implement the import and export functions of form data.

The above is the detailed content of Using Java to develop the import and export functions of form data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!