一、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);

}

}

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

  1. java超级记事本_使用java实现记事本(超详细解释)

    实验课老师要求写的,学习了网上n多大佬的博客后,自己写了一个简单的记事本程序 效果图: 实现代码: package test; import java.awt.*; import java.awt.e ...

  2. java中读取excel数据类型_在Java中读取Excel文件的内容

    利用JExcelApi来动态生成excel文档 首先,请到http://www.andykhan.com/jexcelapi/index.html下载java excel api,主页上同时有比较详细 ...

  3. JAVA 的读取Excel方法_纯Java的方式读取excel2007

    * 首先介绍excel2007文件的格式,这里单只工作表文件,不包括加载宏的以及其他格式的,即.xlsx扩展名的 * 你可以把Book1.xlsx这个文件用解压缩文件打开,这是office2007的新 ...

  4. java 新闻编辑_使用 Java 构建你自己的文本编辑器|Linux 中国

    导读:有时候,除你自己外,没有人能制作你所梦想的工具.以下是如何开始构建你自己的文本编辑器. 本文字数:9393,阅读时长大约:12分钟 https://linux.cn/article-13038- ...

  5. cmd 将文件夹下文件剪切到另外一个文件_总结java中文件拷贝剪切的5种方式-JAVA IO基础总结第五篇...

    本文是Java IO总结系列篇的第5篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  6. java使用缓冲区读取文件_在Java中使用Google的协议缓冲区

    java使用缓冲区读取文件 最近发布了 有效的Java第三版 ,我一直对确定此类Java开发书籍的更新感兴趣,该书籍的最新版本仅通过Java 6进行了介绍 . 在此版本中,显然存在与Java 7 , ...

  7. java++记录+运行_记录java+testng运行selenium(三)---xml、ini、excel、日志等配置

    一: ini文件 ini目前只用处存储浏览类型及需要打开的url,ini文件放在configs文件夹下面. 读取ini代码如下: 1 packagetoolskit.documents;2 3 imp ...

  8. java console输出_将java console的输出写入文件

    FileOutputStream bos = new FileOutputStream("output.txt"); System.setOut(new PrintStream(b ...

  9. java在客户端生成文件_用Java编写创建一对客户端/服务器程序,利用数据报将一个文件从一台主机传送到另一...

    展开全部 下面是e5a48de588b63231313335323631343130323136353331333337386564我自己写的一个读取并显示txt文件的demo,希望对您有帮助. pu ...

最新文章

  1. 转:人气资源大集合~~~2014年12月
  2. Java存储密码用字符数组
  3. 第五章 MVC之Bundle详解
  4. 信息流服务器哪种好,选购存储服务器需要注意六大关键因素,你知道几个?
  5. 将一个对象的空值全部设置为null
  6. swagger map示例_Android Google Map Street View示例
  7. [libtorrent] tracker 逻辑及源码解析(调试)
  8. 工业软件研究框架_现在设计一架飞机时会用哪些软件?免费还开源!|莱特湾出品...
  9. Java NIO?看这一篇就够了!
  10. GBase 8atmp 目录权限改变导致加载失败
  11. 计算机之父图灵成为新50英镑“代言人”,吴恩达发推:Wonderful!
  12. linux下10款markdown软件
  13. WIN10下删除Hiberfil.sys文件
  14. 报表工具——开源还是商用
  15. 【SLAM学习笔记】12-ORB_SLAM3关键源码分析⑩ Optimizer(七)地图融合优化
  16. Go语言核心之美 3.2-slice切片
  17. 华为服务器设备型号查询,查询服务器型号和操作系统
  18. gradle下载安装(个人记录)
  19. 周志华西瓜书学习笔记(一)
  20. 经历两个月茫然期后粪发图强,四面美团定级3-1,拿到35*16offer

热门文章

  1. 智能家居网络安全攻与防
  2. USB Type-C接口(1)——硬件/Lenovo
  3. 直男 or Gay?看看你的DNA标记!
  4. 瑞吉外卖之 redis优化缓存
  5. 汇总|CVPR 2021 自动驾驶相关论文
  6. 应该怎样庆祝自己的生日更能体现生命的意义?
  7. UE4/5 学习笔记*Note7:关于各种无缝动画的衔接问题_2022/8/14
  8. val_acc一直不变
  9. html 表单提交 地址栏 显示=%cc%ed%bc%d3 ,html--表单(示例代码)
  10. 九年义务教育的精英,遇上十年寒窗苦读的翘楚,必将擦出耀眼的火花!