Java操作Excel
来源:互联网 发布:java trim函数 编辑:程序博客网 时间:2024/05/21 11:05
package com.wll.excelAll;import java.io.File;import java.io.FileInputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import java.text.SimpleDateFormat;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.usermodel.WorkbookFactory;import org.apache.poi.ss.usermodel.DateUtil;public class readExcel { public static void main(String[] args) { // 需要读取的文件 readExcel.read("lib/test.xlsx"); } public static void read(String path) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); try { // 同时支持Excel 2003、2007、2010 File excelFile = new File(path); // 创建文件对象 FileInputStream is = new FileInputStream(excelFile); // 文件流 Workbook workbook = WorkbookFactory.create(is); // 支持03 07 10格式 int sheetCount = workbook.getNumberOfSheets(); // 工作簿sheet 的数量 默认3张// 遍历每个Sheet for (int i = 0; i < 1; i++) { Connection connection = null; connection = readExcel.getCon();// 获取数据库链接 StringBuffer ssBuffer = new StringBuffer(); String string = ""; Sheet sheet = workbook.getSheetAt(i); int rowCount = sheet.getPhysicalNumberOfRows(); // 获取总行数 13 // 遍历每一行 for (int r = 0; r < rowCount; r++) { StringBuffer sBuffer = new StringBuffer("insert into test values('"); Row row = sheet.getRow(r); int cellCount = row.getPhysicalNumberOfCells(); // 获取总列数 4 // 遍历每一列 for (int c = 0; c < cellCount; c++) { Cell cell = row.getCell(c); int cellType = cell.getCellType(); String cellValue = null; switch (cellType) { case Cell.CELL_TYPE_STRING: // 文本 字符串型 1 cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: // 数字、日期 数值型 0 if (DateUtil.isCellDateFormatted(cell)) { cellValue = fmt.format(cell.getDateCellValue()); // 日期型 } else { cellValue = String.valueOf(cell.getNumericCellValue()); // 数字 } break; case Cell.CELL_TYPE_BOOLEAN: // 布尔型 布尔型 4 cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_BLANK: // 空白 空值 3 cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_ERROR: // 错误 错误 5 cellValue = "错误"; break; case Cell.CELL_TYPE_FORMULA: // 公式型 2 cellValue = "公式"; break; default: cellValue = "错误"; } System.out.print(cellValue + " "); sBuffer.append(cellValue + "','"); } System.out.println(); // 拼接执行的SQL语句 string = sBuffer.toString().substring(0, sBuffer.toString().lastIndexOf(",")); string = string + ");"; System.out.println("---sql:" + string); Statement st = connection.createStatement(); st.execute(string); ssBuffer.append(string); } connection.close(); } } catch (Exception e) { e.printStackTrace(); } } public static Connection getCon() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.err.println("---error:驱动加载失败"); e.printStackTrace(); } try { connection = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "root"); } catch (SQLException e) { System.err.println("---error:获取链接失败"); e.printStackTrace(); } System.out.println("-----获取链接成功!"); return connection; }}
0 0
- Java Excel 操作excel
- JXL操作Excel java 操作excel
- Java操作Excel,Word
- java操作excel
- java操作Excel
- [JAVA]Excel 文档操作
- JAVA 操作EXCEL
- JAVA操作EXCEL文件
- java操作Excel
- java如何操作Excel
- java 操作 excel
- java操作EXCEL表格
- JAVA 操作EXCEL(转)
- java操作excel
- java操作excel
- JAVA 操作EXCEL文件
- java操作excel表格。
- Java操作Excel,Word
- AdMob(app内嵌广告)横幅广告
- 顺序表实现栈的基本操作
- 《丁小柒的JavaScript大神之路》----javascript历史
- MTU & MSS 详解记录
- FlaskWeb开发
- Java操作Excel
- transmac
- grails的测试
- AdMob(app内嵌广告)插页广告
- 在页面显示Struts2提供的拦截器显示拦截成功的提示信息的步骤
- 对工厂模式与单例模式的理解
- cent os 5 u盘安装
- bzoj3631: [JLOI2014]松鼠的新家
- hdoj 1568 Fibonacci (斐波那契数列通项公式+取对数)