import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import com.dao.WageDao;
public class XlsUtils {
public static void getXls() throws Exception {
XSSFWorkbook book = new XSSFWorkbook("e://工资模板.xlsx");//获得文件
XSSFSheet sheet = book.getSheetAt(0);//获得第一个工作表
XSSFRow title = sheet.getRow(0);//获得标题行
int titleCels = title.getLastCellNum();//获得字段总数
String[] v = new String[titleCels] ;//定义一个数组
int rows = sheet.getLastRowNum();//获得总行数
System.out.println(rows);
for (int i = 1; i <= rows; i++) {//遍历将数据装进数组
XSSFRow row = sheet.getRow(i);
int cels = row.getLastCellNum();
for (int j = 0; j < cels; j++) {
String result = "";
XSSFCell cel = row.getCell(j);
result = getStringCellValue(cel);
v[j]=result;
}
new WageDao().insert(v);//将数级插入数据库。
}
}
private static String getStringCellValue(XSSFCell cell) {//将XLSX内容转为STRING
String strCell = "";
int type = 0;
try {
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case XSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
} catch (Exception e) {
if (e.getMessage()==null) {
strCell = "0";
}
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}
public static void main(String[] args) throws Exception {
getXls();
}
}
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.apache.commons.dbutils.QueryRunner;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
import com.utils.ConnUtils;
import com.utils.XlsUtils;
public class WageDao {//传过来的数组进行处理。
public int insert(String[] v) throws Exception{
Connection conn = ConnUtils.getConnection();
String sql = "insert into [test].[dbo].[RL_Wage](日期,工号,姓名,固定津贴,工龄工资,预发工资,星级奖,节日加班费,超时加班费,计提工资,考核奖,空程补贴,职务补贴,会议补贴,单班补贴,超定额补贴,餐费补贴,其他补贴,高温补贴,全勤奖,安全服务奖,节能降耗奖,爱车例保奖,年休假工资,病假工资,工伤假工资,公假工资,节日慰问金,其他奖金1,其他奖金2,补发工资,减多发工资,应发工资,扣工会费,扣互助会会费,代扣房租,代扣事故费,代扣伙食费,代扣其他,减旷工工资,代扣养老保险,代扣医疗保险,代扣失业保险,代扣住房公积金,代缴个税,实发工资,生日慰问金,保健费,其他福利费,传行工资)" +
"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < 50; i++) {
ps.setString(i+1,v[i]);
}
int row= ps.executeUpdate();
ps.close();
conn.close();
return row;
}
}