使⽤java进⾏excel读取和写⼊1:添加处理excel的依赖jar包
<!-- 引⼊poi,解析workbook视图 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!-- 处理excel和上⾯功能是⼀样的-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.10</version>
</dependency>
2:向excel中写⼊内容的类
  WriteExcel.java
package ller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WriteExcel {
private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx";
public static void main(String[] args) {
Map<String, String> dataMap=new HashMap<String, String>();
dataMap.put("BankName", "BankName");
dataMap.put("Addr", "Addr");
dataMap.put("Phone", "Phone");
List<Map> list=new ArrayList<Map>();
list.add(dataMap);
writeExcel(list, 3, "D:/writeExcel.xlsx");
}
public static void writeExcel(List<Map> dataList, int cloumnCount,String finalXlsxPath){
OutputStream out = null;
try {
// 获取总列数
int columnNumCount = cloumnCount;
// 读取Excel⽂档
File finalXlsxFile = new File(finalXlsxPath);
Workbook workBook = getWorkbok(finalXlsxFile);
// sheet 对应⼀个⼯作页
Sheet sheet = SheetAt(0);
/**
* 删除原有数据,除了属性列
*/
int rowNumber = LastRowNum();    // 第⼀⾏从0开始算
System.out.println("原始数据总⾏数,除属性列:" + rowNumber);
for (int i = 1; i <= rowNumber; i++) {
Row row = Row(i);
}
// 创建⽂件输出流,输出电⼦表格:这个必须有,否则你在sheet上做的任何操作都不会有效
out =  new FileOutputStream(finalXlsxPath);
workBook.write(out);
/**
* 往Excel中写新数据
*/
for (int j = 0; j < dataList.size(); j++) {
// 创建⼀⾏:从第⼆⾏开始,跳过属性列
Row row = ateRow(j + 1);
// 得到要插⼊的每⼀条记录
Map dataMap = (j);
String name = ("BankName").toString();
String address = ("Addr").toString();
String phone = ("Phone").toString();
for (int k = 0; k <= columnNumCount; k++) {
// 在⼀⾏内循环
Cell first = ateCell(0);
first.setCellValue(name);
Cell second = ateCell(1);
second.setCellValue(address);
Cell third = ateCell(2);
third.setCellValue(phone);
}
}
// 创建⽂件输出流,准备输出电⼦表格:这个必须有,否则你在sheet上做的任何操作都不会有效            out =  new FileOutputStream(finalXlsxPath);
workBook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(out != null){
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("数据导出成功");
}
/**
* 判断Excel的版本,获取Workbook
* @param in
* @param filename
* @return
* @throws IOException
*/
public static Workbook getWorkbok(File file) throws IOException{
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
Name().endsWith(EXCEL_XLS)){    //Excel 2003
wb = new HSSFWorkbook(in);
}else Name().endsWith(EXCEL_XLSX)){    // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}
}
3:读取Excel中的数据,并写⼊list中
package ller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
ad.biff.BiffException;
public class ReadExcel {
public static void main(String[] args) {
ReadExcel obj = new ReadExcel();
// 此处为我创建Excel路径:E:/zhanhj/studysrc/jxl下
File file = new File("D:/readExcel.xls");
List excelList = adExcel(file);java创建文件
System.out.println("list中的数据打印出来");
for (int i = 0; i < excelList.size(); i++) {
List list = (List) (i);
for (int j = 0; j < list.size(); j++) {
System.out.(j));
}
System.out.println();
}
}
// 去读Excel的⽅法readExcel,该⽅法的⼊⼝参数为⼀个File对象
public List readExcel(File file) {
try {
// 创建输⼊流,读取Excel
InputStream is = new AbsolutePath());
// jxl提供的Workbook类
Workbook wb = Workbook(is);
// Excel的页签数量
int sheet_size = wb.getNumberOfSheets();
for (int index = 0; index < sheet_size; index++) {
List<List> outerList=new ArrayList<List>();
// 每个页签创建⼀个Sheet对象
Sheet sheet = wb.getSheet(index);
/
/ Rows()返回该页的总⾏数
for (int i = 0; i < Rows(); i++) {
List innerList=new ArrayList();
// Columns()返回该页的总列数
for (int j = 0; j < Columns(); j++) {
String cellinfo = Cell(j, i).getContents();
if(cellinfo.isEmpty()){
continue;
}
innerList.add(cellinfo);
System.out.print(cellinfo);
}
outerList.add(i, innerList);
System.out.println();
}
return outerList;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
4:在D盘下⾯创建readExcel.xls(有内容)和writeExcel.xlsx即可

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。