linux java excel文件_使用Java语言将excel中读取到的内容导入Linux的文件中

该博客介绍了如何使用Java的Apache POI库读取Excel文件,并将内容追加到Linux系统的文本文件中,涉及maven配置、Excel读取及文件写入操作。
摘要由CSDN通过智能技术生成

一、maven配置

导入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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值