导入excel表格需要使用的依赖:
org.apache.poi
poi
4.0.0
org.apache.poi
poi-ooxml
4.0.0
package utils;
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class AddData {
public static String userPath = "C:/Users/DELL/Desktop/user";
public static String excelPath = "C:/Users/DELL/Desktop/user.xlsx";
public static BufferedReader userIn = null;
public static BufferedWriter userOut = null;
public static int userNum ;
public static int excelUserNum;
public static String userContent = null;
public static String excelContent = null;
public static String readUserInfo(String userPath){
StringBuilder userInfo = new StringBuilder(); //将读到的user表信息放在userInfo中
try {
userIn = new BufferedReader(new InputStreamReader(
new FileInputStream(userPath),"GBK"));
userNum = Integer.parseInt(userIn.readLine());
System.out.println(userNum);
String temp = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((temp = userIn.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + temp);
userInfo.append(temp + "\n");
line++;
}
return userInfo.toString();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
userIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return userInfo.toString();
}
public static String readExcelInfo(String excelPath){
StringBuilder excelInfo = new StringBuilder(); //将读到的excel表信息放在excelInfo中
try {
File excel = new File(excelPath);
if (excel.isFile() && excel.exists()) { //判断文件是否存在
String[] split = excel.getName().split("\\."); //.是特殊字符,需要转义!!!!!
Workbook wb;
//根据文件后缀(xls/xlsx)进行判断
if ( "xls".equals(split[1])){
FileInputStream fis = new FileInputStream(excel); //文件流对象
wb = new HSSFWorkbook(fis);
}else if ("xlsx".equals(split[1])){
wb = new XSSFWorkbook(excel);
}else {
System.out.println("文件类型错误!");
throw new Exception("文件类型错误");
}
//开始解析
Sheet sheet = wb.getSheetAt(0); //读取sheet 0
int firstRowIndex = sheet.getFirstRowNum()+1; //第一行是列名,所以不读
int lastRowIndex = sheet.getLastRowNum();
excelUserNum = lastRowIndex;
System.out.println("firstRowIndex: "+firstRowIndex);
System.out.println("lastRowIndex: "+lastRowIndex);
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) { //遍历行
System.out.println("rIndex: " + rIndex );
Row row = sheet.getRow(rIndex);
if (row != null) {
int firstCellIndex = row.getFirstCellNum();
int lastCellIndex = row.getLastCellNum();
for(int i = firstCellIndex; i < 16 + firstCellIndex; i++){
if (i == 0) { //遍历列lastCellIndex
Cell cell = row.getCell(i);
if (cell != null) {
cell.setCellType(CellType.STRING);
excelInfo.append(cell.toString() + "\n"); //学号作为ID
excelInfo.append(cell.toString() + "\n"); //学号也作为密码
}
}else if(i == 1){
Cell cell = row.getCell(i);
if (cell != null) {
cell.setCellType(CellType.STRING);
excelInfo.append(cell.toString()+ "\n"); //姓名
}
}else if(i == 15){
excelInfo.append("localhost\n"); //最后一行信息
}else{
excelInfo.append("1\n"); //其余行默认为1
}
}
}
}
return excelInfo.toString();
} else {
System.out.println("找不到指定的文件");
throw new Exception("找不到指定的文件");
}
} catch (Exception e) {
e.printStackTrace();
}
return excelInfo.toString();
}
public static void writeUserInfo(String userPath,String excelPath) { //向文件末尾添加数据
userContent = readUserInfo(userPath);
excelContent = readExcelInfo(excelPath);
int totalNum = userNum + excelUserNum;
try {
userOut = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(userPath),"GBK"));
userOut.write(totalNum + "\n"); //写文件第一行用户数
userOut.write(userContent); //写原文件中的信息
userOut.write(excelContent); //写excel文件中的信息
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
userOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
writeUserInfo(userPath,excelPath);
}
}