{"id":166615,"date":"2024-11-05T17:47:25","date_gmt":"2024-11-05T09:47:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/166615\/"},"modified":"2024-11-05T17:47:26","modified_gmt":"2024-11-05T09:47:26","slug":"%e5%bf%ab%e9%80%9f%e5%ad%b8%e7%bf%92%ef%bc%9a%e4%bd%bf%e7%94%a8swing%e5%b0%8e%e5%87%baexcel%e6%96%87%e4%bb%b6%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e3%80%82-swing%e5%be%9e%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/166615\/","title":{"rendered":"\u5feb\u901f\u5b78\u7fd2\uff1a\u4f7f\u7528Swing\u5c0e\u51faExcel\u6587\u4ef6\u5230\u6578\u64da\u5eab\u3002 (swing\u5f9e\u6578\u64da\u5eab\u4e2d\u5c0e\u51faexcel)"},"content":{"rendered":"<h1 id=\"%e5%bf%ab%e9%80%9f%e5%ad%b8%e7%bf%92%ef%bc%9a%e4%bd%bf%e7%94%a8swing%e5%b0%8e%e5%87%baexcel%e6%96%87%e4%bb%b6%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-UnJLVhOOgo\">\u5feb\u901f\u5b78\u7fd2\uff1a\u4f7f\u7528Swing\u5c0e\u51faExcel\u6587\u4ef6\u5230\u6578\u64da\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u5c07\u6578\u64da\u5f9eExcel\u6587\u4ef6\u5c0e\u5165\u6578\u64da\u5eab\u7684\u9700\u6c42\u65e5\u76ca\u589e\u52a0\u3002Java Swing\u4f5c\u70ba\u4e00\u500b\u5f37\u5927\u7684\u5716\u5f62\u7528\u6236\u754c\u9762\uff08GUI\uff09\u5de5\u5177\u5305\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5275\u5efa\u7528\u6236\u53cb\u597d\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u4e26\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Swing\u5f9eExcel\u6587\u4ef6\u5c0e\u51fa\u6578\u64da\u5230\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-UnJLVhOOgo\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u958b\u767c\u74b0\u5883\u4e2d\u5df2\u7d93\u5b89\u88dd\u4e86\u4ee5\u4e0b\u5de5\u5177\u548c\u5eab\uff1a<\/p>\n<ul>\n<li>Java Development Kit (JDK)<\/li>\n<li>Apache POI\uff1a\u7528\u65bc\u8b80\u53d6\u548c\u5beb\u5165Excel\u6587\u4ef6\u7684Java\u5eab<\/li>\n<li>JDBC\u9a45\u52d5\u7a0b\u5e8f\uff1a\u7528\u65bc\u9023\u63a5\u6578\u64da\u5eab\u7684\u9a45\u52d5\u7a0b\u5e8f<\/li>\n<\/ul>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%80%ef%bc%9a%e8%a8%ad%e7%bd%aeswing%e7%95%8c%e9%9d%a2-UnJLVhOOgo\">\u6b65\u9a5f\u4e00\uff1a\u8a2d\u7f6eSwing\u754c\u9762<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7684Swing\u754c\u9762\uff0c\u8b93\u7528\u6236\u80fd\u5920\u9078\u64c7Excel\u6587\u4ef6\u4e26\u89f8\u767c\u5c0e\u5165\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684Swing\u754c\u9762\u4ee3\u78bc\uff1a<\/p>\n<pre><code>\nimport javax.swing.*;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\n\npublic class ExcelImporter {\n    public static void main(String[] args) {\n        JFrame frame = new JFrame(\"Excel\u5c0e\u5165\u5668\");\n        JButton button = new JButton(\"\u9078\u64c7Excel\u6587\u4ef6\");\n        \n        button.addActionListener(new ActionListener() {\n            @Override\n            public void actionPerformed(ActionEvent e) {\n                JFileChooser fileChooser = new JFileChooser();\n                int returnValue = fileChooser.showOpenDialog(null);\n                if (returnValue == JFileChooser.APPROVE_OPTION) {\n                    \/\/ \u5728\u9019\u88e1\u8655\u7406\u6587\u4ef6\u5c0e\u5165\n                }\n            }\n        });\n        \n        frame.add(button);\n        frame.setSize(300, 200);\n        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        frame.setVisible(true);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%ba%8c%ef%bc%9a%e8%ae%80%e5%8f%96excel%e6%96%87%e4%bb%b6-UnJLVhOOgo\">\u6b65\u9a5f\u4e8c\uff1a\u8b80\u53d6Excel\u6587\u4ef6<\/h2>\n<p>\u4f7f\u7528Apache POI\u5eab\u4f86\u8b80\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u8b80\u53d6Excel\u6587\u4ef6\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nimport org.apache.poi.ss.usermodel.*;\nimport org.apache.poi.xssf.usermodel.XSSFWorkbook;\n\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.IOException;\n\npublic class ExcelReader {\n    public static void readExcel(File file) {\n        try (FileInputStream fis = new FileInputStream(file);\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<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%89%ef%bc%9a%e5%b0%87%e6%95%b8%e6%93%9a%e5%b0%8e%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-UnJLVhOOgo\">\u6b65\u9a5f\u4e09\uff1a\u5c07\u6578\u64da\u5c0e\u5165\u6578\u64da\u5eab<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5c07\u8b80\u53d6\u5230\u7684\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528JDBC\u5c07\u6578\u64da\u63d2\u5165\u6578\u64da\u5eab\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nimport java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.PreparedStatement;\nimport java.sql.SQLException;\n\npublic class DatabaseImporter {\n    private static final String URL = \"jdbc:mysql:\/\/localhost:3306\/your_database\";\n    private static final String USER = \"your_username\";\n    private static final String PASSWORD = \"your_password\";\n\n    public static void insertData(String data) {\n        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {\n            String sql = \"INSERT INTO your_table (column_name) VALUES (?)\";\n            PreparedStatement statement = connection.prepareStatement(sql);\n            statement.setString(1, data);\n            statement.executeUpdate();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e5%9b%9b%ef%bc%9a%e6%95%b4%e5%90%88%e6%89%80%e6%9c%89%e5%8a%9f%e8%83%bd-UnJLVhOOgo\">\u6b65\u9a5f\u56db\uff1a\u6574\u5408\u6240\u6709\u529f\u80fd<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5c07\u4ee5\u4e0a\u6240\u6709\u529f\u80fd\u6574\u5408\u5230Swing\u754c\u9762\u4e2d\uff0c\u5be6\u73fe\u5f9eExcel\u6587\u4ef6\u5230\u6578\u64da\u5eab\u7684\u5b8c\u6574\u6d41\u7a0b\u3002\u4ee5\u4e0b\u662f\u6574\u5408\u5f8c\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nimport javax.swing.*;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.io.File;\n\npublic class ExcelImporter {\n    public static void main(String[] args) {\n        JFrame frame = new JFrame(\"Excel\u5c0e\u5165\u5668\");\n        JButton button = new JButton(\"\u9078\u64c7Excel\u6587\u4ef6\");\n        \n        button.addActionListener(new ActionListener() {\n            @Override\n            public void actionPerformed(ActionEvent e) {\n                JFileChooser fileChooser = new JFileChooser();\n                int returnValue = fileChooser.showOpenDialog(null);\n                if (returnValue == JFileChooser.APPROVE_OPTION) {\n                    File selectedFile = fileChooser.getSelectedFile();\n                    ExcelReader.readExcel(selectedFile);\n                    \/\/ \u5047\u8a2d\u8b80\u53d6\u5230\u7684\u6578\u64da\u70badata\n                    String data = \"\u8b80\u53d6\u5230\u7684\u6578\u64da\"; \/\/ \u9019\u88e1\u61c9\u8a72\u662f\u5be6\u969b\u8b80\u53d6\u7684\u6578\u64da\n                    DatabaseImporter.insertData(data);\n                }\n            }\n        });\n        \n        frame.add(button);\n        frame.setSize(300, 200);\n        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        frame.setVisible(true);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-UnJLVhOOgo\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528Java Swing\u5f9eExcel\u6587\u4ef6\u5c0e\u51fa\u6578\u64da\u5230\u6578\u64da\u5eab\u3002\u9019\u4e00\u904e\u7a0b\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u9084\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u6578\u64da\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6578\u64da\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9019\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u60a8\u7684\u6578\u64da\u5b89\u5168\u4e14\u9ad8\u6548\u5730\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swing\u5c07Excel\u6587\u4ef6\u5c0e\u51fa\u5230\u6578\u64da\u5eab\uff0c\u7c21\u5316\u6578\u64da\u8655\u7406\u6d41\u7a0b\uff0c\u63d0\u5347\u5de5\u4f5c\u6548\u7387\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-166615","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166615","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=166615"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166615\/revisions"}],"predecessor-version":[{"id":166616,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/166615\/revisions\/166616"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=166615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=166615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=166615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}