{"id":183602,"date":"2024-11-10T06:11:24","date_gmt":"2024-11-09T22:11:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183602\/"},"modified":"2024-11-10T06:11:24","modified_gmt":"2024-11-09T22:11:24","slug":"%e4%bd%bf%e7%94%a8apache-poi%e9%80%b2%e8%a1%8c%e5%be%aa%e7%92%b0%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-apache-poi%e5%be%aa%e7%92%b0%e5%af%ab%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183602\/","title":{"rendered":"\u4f7f\u7528Apache POI\u9032\u884c\u5faa\u74b0\u5beb\u5165\u6578\u64da\u5eab\u64cd\u4f5c (apache poi\u5faa\u74b0\u5beb\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8apache-poi%e9%80%b2%e8%a1%8c%e5%be%aa%e7%92%b0%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-cGTGwqvyWo\">\u4f7f\u7528Apache POI\u9032\u884c\u5faa\u74b0\u5beb\u5165\u6578\u64da\u5eab\u64cd\u4f5c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u7684\u7ba1\u7406\u548c\u8655\u7406\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Apache POI\u662f\u4e00\u500b\u5f37\u5927\u7684Java\u5eab\uff0c\u5c08\u9580\u7528\u65bc\u8b80\u53d6\u548c\u5beb\u5165Microsoft Office\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u7279\u5225\u662fExcel\u6587\u4ef6\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Apache POI\u9032\u884c\u5faa\u74b0\u5beb\u5165\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u9ad8\u6548\u5730\u8655\u7406\u6578\u64da\u3002<\/p>\n<h2 id=\"apache-poi%e7%b0%a1%e4%bb%8b-cGTGwqvyWo\">Apache POI\u7c21\u4ecb<\/h2>\n<p>Apache POI\u662f\u4e00\u500b\u958b\u6e90\u7684Java\u5eab\uff0c\u5141\u8a31\u958b\u767c\u8005\u8b80\u53d6\u548c\u5beb\u5165Microsoft Office\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5305\u62ecExcel\u3001Word\u548cPowerPoint\u3002\u5c0d\u65bc\u8655\u7406Excel\u6587\u4ef6\uff0cApache POI\u63d0\u4f9b\u4e86\u5169\u500b\u4e3b\u8981\u7684API\uff1aHSSF\uff08\u7528\u65bc.xls\u6587\u4ef6\uff09\u548cXSSF\uff08\u7528\u65bc.xlsx\u6587\u4ef6\uff09\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u64cd\u4f5cExcel\u6587\u4ef6\u4e2d\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e6%a6%82%e8%bf%b0-cGTGwqvyWo\">\u6578\u64da\u5eab\u64cd\u4f5c\u6982\u8ff0<\/h2>\n<p>\u5728\u8a31\u591a\u61c9\u7528\u4e2d\uff0c\u5c07Excel\u4e2d\u7684\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\u3002\u9019\u901a\u5e38\u6d89\u53ca\u5230\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li>\u8b80\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6578\u64da\u3002<\/li>\n<li>\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\u3002<\/li>\n<li>\u5c07\u8b80\u53d6\u7684\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e7%8f%be%e6%ad%a5%e9%a9%9f-cGTGwqvyWo\">\u5be6\u73fe\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e8%ae%80%e5%8f%96excel%e6%96%87%e4%bb%b6-cGTGwqvyWo\">1. \u8b80\u53d6Excel\u6587\u4ef6<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u4f7f\u7528Apache POI\u8b80\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>\nimport org.apache.poi.ss.usermodel.*;\nimport org.apache.poi.xssf.usermodel.XSSFWorkbook;\n\nimport java.io.FileInputStream;\nimport java.io.IOException;\n\npublic class ExcelReader {\n    public static void main(String[] args) {\n        String excelFilePath = \"data.xlsx\";\n        try (FileInputStream fis = new FileInputStream(excelFilePath);\n             Workbook workbook = new XSSFWorkbook(fis)) {\n             \n            Sheet sheet = workbook.getSheetAt(0);\n            for (Row row : sheet) {\n                for (Cell cell : row) {\n                    System.out.print(cell.toString() + \"t\");\n                }\n                System.out.println();\n            }\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n<h3 id=\"2-%e5%bb%ba%e7%ab%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-cGTGwqvyWo\">2. \u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u9019\u88e1\u4ee5MySQL\u70ba\u4f8b\uff1a<\/p>\n<pre><code>\nimport java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\n\npublic class DatabaseConnection {\n    public static Connection getConnection() {\n        String url = \"jdbc:mysql:\/\/localhost:3306\/your_database\";\n        String user = \"your_username\";\n        String password = \"your_password\";\n        Connection connection = null;\n        try {\n            connection = DriverManager.getConnection(url, user, password);\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n        return connection;\n    }\n}\n<\/code><\/pre>\n<h3 id=\"3-%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-cGTGwqvyWo\">3. \u5beb\u5165\u6578\u64da\u5eab<\/h3>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u5c07\u8b80\u53d6\u7684\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u5c07\u6578\u64da\u63d2\u5165\u6578\u64da\u5eab\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>\nimport java.sql.Connection;\nimport java.sql.PreparedStatement;\nimport java.sql.SQLException;\n\npublic class DataInserter {\n    public static void insertData(String data) {\n        String insertSQL = \"INSERT INTO your_table (column_name) VALUES (?)\";\n        try (Connection connection = DatabaseConnection.getConnection();\n             PreparedStatement preparedStatement = connection.prepareStatement(insertSQL)) {\n             \n            preparedStatement.setString(1, data);\n            preparedStatement.executeUpdate();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e5%ae%8c%e6%95%b4%e7%a4%ba%e4%be%8b-cGTGwqvyWo\">\u5b8c\u6574\u793a\u4f8b<\/h2>\n<p>\u5c07\u4e0a\u8ff0\u6b65\u9a5f\u7d50\u5408\u8d77\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u5b8c\u6574\u7684\u7a0b\u5e8f\uff0c\u5f9eExcel\u6587\u4ef6\u4e2d\u8b80\u53d6\u6578\u64da\u4e26\u5c07\u5176\u5beb\u5165\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>\npublic class ExcelToDatabase {\n    public static void main(String[] args) {\n        String excelFilePath = \"data.xlsx\";\n        try (FileInputStream fis = new FileInputStream(excelFilePath);\n             Workbook workbook = new XSSFWorkbook(fis)) {\n             \n            Sheet sheet = workbook.getSheetAt(0);\n            for (Row row : sheet) {\n                for (Cell cell : row) {\n                    DataInserter.insertData(cell.toString());\n                }\n            }\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-cGTGwqvyWo\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528Apache POI\u9032\u884c\u5faa\u74b0\u5beb\u5165\u6578\u64da\u5eab\u64cd\u4f5c\u662f\u4e00\u500b\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u8655\u7406Excel\u6578\u64da\u4e26\u5c07\u5176\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u9084\u6e1b\u5c11\u4e86\u624b\u52d5\u8f38\u5165\u6578\u64da\u7684\u932f\u8aa4\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Apache POI\u9032\u884c\u5faa\u74b0\u5beb\u5165\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316Excel\u8207\u6578\u64da\u5eab\u4e4b\u9593\u7684\u4ea4\u4e92\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-183602","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=183602"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183602\/revisions"}],"predecessor-version":[{"id":183603,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183602\/revisions\/183603"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}