Java言語を使ってエクセルで読み取った内容をLinuxのファイルに導入します.
6342 ワード
一、maven配置
エクセル表の導入に必要な依存性:
エクセル表の導入に必要な依存性:
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 + "
");
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() + "
"); // ID
excelInfo.append(cell.toString() + "
"); //
}
}else if(i == 1){
Cell cell = row.getCell(i);
if (cell != null) {
cell.setCellType(CellType.STRING);
excelInfo.append(cell.toString()+ "
"); //
}
}else if(i == 15){
excelInfo.append("localhost
"); //
}else{
excelInfo.append("1
"); // 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 + "
"); //
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);
}
}