Java实现EXCEL操作(1)
1、实现方法:
现在有三种方法去实现:jxl 、poi 、 FastExcel:97~2003
在这里只讲poi实现方法。poi的包可以去Apache官网上去下载:http://poi.apache.org/download.html
2、poi实现
【1】低版本的导入导出方法:
需要导入jar包:
poi-3.1.5.jar
commons-io-2.2。jar
生成EXCEL:
class="java">import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PoiExpExcel {
/**
* 生成Excel文件
*/
public static void main(String[] args) {
String[] title = {"id", "name", "sex"};
//创建工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个工作表sheet
HSSFSheet sheet = workbook.createSheet();
//创建爱你第一行
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
//插入第一行数据id、name、sex
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
//追加数据
for(int i = 1; i <= 10; i++) {
HSSFRow nextrow = sheet.createRow(i);
HSSFCell cell2 = nextrow.createCell(0);
cell2.setCellValue("" + i);
cell2 =nextrow.createCell(1);
cell2.setCellValue("user" + i);
cell2 =nextrow.createCell(2);
cell2.setCellValue("男");
}
//创建一个文件
File file =new File("C:/Users/Administrator/Desktop/demo1.xls"); //在这里填写存放路径
try {
file.createNewFile();
FileOutputStream stream = FileUtils.openOutputStream(file);
workbook.write(stream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解析EXCEL:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PoiReadExcel {
public static void main(String[] args) {
/**
* 解析Excel文件
*/
File file = new File("C:/Users/Administrator/Desktop/demo1.xls");
try {
HSSFWorkbook workbook =
new HSSFWorkbook(FileUtils.openInputStream(file));
//获取第一个工作表workbook.getSheet("Sheet0");
// HSSFSheet sheet = workbook.getSheet("Sheet0");
//读取默认第一个工作表sheet
HSSFSheet sheet = workbook.getSheetAt(0);
int firstRowNum = 0;
//获取sheet中最后一行行号
int lastRowNum = sheet.getLastRowNum();
for(int i = firstRowNum; i <= lastRowNum; i++) {
HSSFRow row = sheet.getRow(i);
//获取当前行最后单元格列号
int lastCellNum = row.getLastCellNum();
for(int j = 0; j < lastCellNum; j++) {
HSSFCell cell = row.getCell(j);
String value = cell.getStringCellValue();
System.out.print(value + " ");
}
System.out.println();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
【2】高版本导入导出的方法:
需要导入包:
如附件
生成EXCEL:
import java.io.File;
import java.io.FileOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.poi.xssf.usermodel.*;
public class CopyOfPoiExp07Excel {
/**
* 生成Excel文件
*/
public static void main(String[] args) {
String[] title = {"id", "name", "sex"};
//创建工作薄
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个工作表sheet
XSSFSheet sheet = workbook.createSheet();
//创建爱你第一行
XSSFRow row = sheet.createRow(0);
XSSFCell cell = null;
//插入第一行数据id、name、sex
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
//追加数据
for(int i = 1; i <= 10; i++) {
XSSFRow nextrow = sheet.createRow(i);
XSSFCell cell2 = nextrow.createCell(0);
cell2.setCellValue("" + i);
cell2 =nextrow.createCell(1);
cell2.setCellValue("user" + i);
cell2 =nextrow.createCell(2);
cell2.setCellValue("男");
}
//创建一个文件
File file =new File("C:/Users/Administrator/Desktop/demo1.xlsx");
try {
file.createNewFile();
FileOutputStream stream = FileUtils.openOutputStream(file);
workbook.write(stream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解析EXCEL:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CopyOfPoiRead07Excel {
public static void main(String[] args) {
//需要解析的Excel文件
File file = new File("C:/Users/Administrator/Desktop/demo1.xls");
try {
XSSFWorkbook workbook =
new XSSFWorkbook(FileUtils.openInputStream(file));
//获取第一个工作表workbook.getSheet("Sheet0");
// XSSFSheet sheet = workbook.getSheet("Sheet0");
//读取默认第一个工作表sheet
XSSFSheet sheet = workbook.getSheetAt(0);
int firstRowNum = 0;
//获取sheet中最后一行行号
int lastRowNum = sheet.getLastRowNum();
for(int i = firstRowNum; i <= lastRowNum; i++) {
XSSFRow row = sheet.getRow(i);
//获取当前行最后单元格列号
int lastCellNum = row.getLastCellNum();
for(int j = 0; j < lastCellNum; j++) {
XSSFCell cell = row.getCell(j);
String value = cell.getStringCellValue();
System.out.print(value + " ");
}
System.out.println();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
生成文件:
[img][/img]
大小: 25.2 KB
大小: 75 KB
查看图片附件