注意,这里的Excel文件只仅限于以“.xls”为后缀的版本,即“Excel 97-2003 工作簿(*.xls)”版本,其他版本很可能会报错!
一、使用maven导入需要的工具jar包
<!--Excel操作包-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
二、对Excel的一些列操作
1.读取指定的Excel文件信息
代码:
readExcel()方法代码:
/**
* 读取指定Excel文件中的内容
* @param filePath 文件路径
* @param fileName 文件名
*/
public static void readExcel(String filePath, String fileName){
try{
//获取Excel文件
File excelFile = new File(filePath + "\\" + fileName);
//获取Excel工作簿
Workbook wb = Workbook.getWorkbook(excelFile);
//获取索引为0的工作表
Sheet sheet = wb.getSheet(0);
for(int i = 0 ; i < sheet.getRows() ; i++){
for (int j = 0 ; j < sheet.getColumns() ; j++){
//获取j列i行的数据
String content = sheet.getCell(j,i).getContents();
System.out.print(content + "\t");
}
System.out.println();
}
}catch (Exception e){
e.printStackTrace();
}
}
执行代码:
public static void main(String[] args){
System.out.println("***************测试readExcel()方法****************");
readExcel("C:\\Users\\xjfu\\Desktop\\ExcelTest","测试1.xls");
}
2.向指定Excel文件写入信息
代码:
writeExcelFile()方法代码:
/**
* 在指定的Excel(已存在的)文件中写入数据
* @param dirPath excel文件路径
* @param fileName excel文件名(带后缀)
*/
public static void writeExcelFile(String dirPath, String fileName){
WritableWorkbook wb = null;
try {
//获取Excel文件
File excelFile = new File(dirPath + "\\" + fileName);
//获取Excel工作簿
wb = Workbook.createWorkbook(excelFile, Workbook.getWorkbook(excelFile));
//获取下标为0的工作表
WritableSheet sheet = wb.getSheet(0);
/**
* 设置格式:
* WritableFont.ARIAL 字体
* 10 字体大小
* WritableFont.BOLD 加粗
* true 斜体
*/
WritableFont font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, true);
WritableCellFormat cellFormat = new WritableCellFormat(font);
//绘制一个5行5列的数据
for (int row = 0 ; row < 5 ; row++){
for (int col = 0 ; col < 5 ; col++){
Label label = null;
if (row%2 == 0){
//偶数行变格式
label = new Label(col, row, row + "行" + col + "列", cellFormat);
}else{
label = new Label(col, row, row + "行" + col + "列");
}
sheet.addCell(label);
}
}
//将数据写入工作簿
wb.write();
wb.close();
}catch (Exception e){
e.printStackTrace();
}
}
执行代码:
public static void main(String[] args){
System.out.println("***************writeExcelFile()方法****************");
writeExcelFile("C:\\Users\\xjfu\\Desktop\\ExcelTest","测试1.xls");
}
3.创建一个新的Excel文件,并写入信息
代码:
writeExcel()方法代码:
/**
* 新建一个Excel文件,并写入数据
* @param dirPath 新建的excel文件路径
* @param fileName 新建的excel文件名(带后缀)
*/
public static void writeExcel(String dirPath, String fileName){
WritableWorkbook wb = null;
try {
//获取Excel文件
File excelFile = new File(dirPath + "\\" + fileName);
//获取Excel工作簿
wb = Workbook.createWorkbook(excelFile);
//获取下标为0的工作表
WritableSheet sheet = wb.createSheet("新 sheet", 0);
/**
* 设置格式:
* WritableFont.ARIAL 字体
* 10 字体大小
* WritableFont.BOLD 加粗
* true 斜体
*/
WritableFont font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, true);
WritableCellFormat cellFormat = new WritableCellFormat(font);
//居中
cellFormat.setAlignment(Alignment.CENTRE);
//背景色
cellFormat.setBackground(Colour.LIGHT_GREEN);
//绘制一个5行5列的数据
for (int row = 0 ; row < 5 ; row++){
for (int col = 0 ; col < 5 ; col++){
Label label = null;
if (row%2 == 0){
//偶数行变格式
label = new Label(col, row, row + "行" + col + "列", cellFormat);
}else{
label = new Label(col, row, row + "行" + col + "列");
}
sheet.addCell(label);
}
}
//将数据写入工作簿
wb.write();
wb.close();
}catch (Exception e){
e.printStackTrace();
}
}
执行代码:
public static void main(String[] args){
System.out.println("***************writeExcel()方法****************");
writeExcel("C:\\Users\\xjfu\\Desktop\\ExcelTest","测试2.xls");
}
三、码云地址
上面所有实例代码,都存放在个人码云上,地址:
https://gitee.com/fu_xiao_jie/all-util
四、参考其他博客地址
https://vimsky.com/examples/detail/java-class-jxl.write.WritableSheet.html