这一段都是无关主题的废话:
很久没有写Java了,今天由于要给一些参会人员发短信,想偷懒批量读取客人的电话号码,因此写了个Java程序,自己以前有下poi的jar包。本来想在网上直接找一些源码的,结果都不是很好用,所以就来mark以下,方便自己以后使用。
使用的jar包:
链接:https://pan.baidu.com/s/1aokOgH1fkHHlzJbKWuz1Dg
提取码:vbma
请注意这个源码只能读取.xlsx格式的,如果想读取.xls的请将源码中所有XSSF替换为HSSF。
我也会在后文介绍。
源码如下:
package excel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class QueryExcel {
public static void main(String[] args) {
// 读取Excel文件
String filePath = "E:/jar/userInfo.xlsx";
try {
// 得到所有数据
List<String> allData = readExcel(filePath);
StringBuilder builder = new StringBuilder();
for(String data : allData) {
builder.append(data).append(",");
}
String result = builder.toString().substring(0, builder.toString().length() - 1);
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 读取指定Sheet也的内容
*
* @param filepath
* filepath 文件全路径
* @param sheetNo
* sheet序号,从0开始,如果读取全文sheetNo设置null
* @throws IOException
* @throws FileNotFoundException
*/
public static List<String> readExcel(String filepath) throws FileNotFoundException, IOException {
List<String> list = new ArrayList<String>();
// 读取文件
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(filepath));
XSSFSheet sheet = workbook.getSheetAt(0);
int rowNum = sheet.getPhysicalNumberOfRows();
XSSFRow row;
// 第一行单独处理
int index_phone = -1;
XSSFRow firstRow = sheet.getRow(0);
for (Cell rows : firstRow) {
if (rows != null) {
// 找到电话列
index_phone++;
if ("电话号码".equals(rows.getStringCellValue())) {
System.out.println(index_phone);
break;
}
}
}
for (int i = 1; i < rowNum; i++) {
row = sheet.getRow(i);
if(row != null) {
String phoneNum = row.getCell(index_phone).getStringCellValue();
if(phoneNum != null && !"".equals(phoneNum)) {
list.add(phoneNum);
System.out.println(phoneNum);
}
}
}
return list;
}
}
我这个地方是读取一张excel表,并根据第一行的内容,找到“电话号码”这一列,并将这一列中不为空的电话号码找出来,拼接为可以发短信的字符串。
简单讲解代码:
// 第一行单独处理
int index_phone = -1;//存我需要取出的那一行的位置,因为一进入循环就会+1,因此我将初值设为-1
XSSFRow firstRow = sheet.getRow(0);//0就是第一行
for (Cell rows : firstRow) {
if (rows != null) {
// 找到电话列
index_phone++;
if ("电话号码".equals(rows.getStringCellValue())) {//这个地方判断自己需要找到的列
System.out.println(index_phone);
break;
}
}
}
for (int i = 1; i < rowNum; i++) {
row = sheet.getRow(i);
if(row != null) {//由于有些值可能是空的,因此一定要判断
//这个地方容易报错,因为有些时候会把电话号码设为数字,那么就不能用getStringCellValue这个方法,但是用getNumericCellValue读取电话号码会被转为1.36*****这种格式。因此可以先把excel里面的列改为文本,再使用getStringCellValue读取
String phoneNum = row.getCell(index_phone).getStringCellValue();
if(phoneNum != null && !"".equals(phoneNum)) {
list.add(phoneNum);
System.out.println(phoneNum);
}
}
}
这个是我的表格格式,由于信息保密,所有没有给出所有表信息。
如果读取.xlsx采用下面的代码:
// 读取文件
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(filepath));
XSSFSheet sheet = workbook.getSheetAt(0);
int rowNum = sheet.getPhysicalNumberOfRows();
如果读取.xls采用下面的代码:
package excel;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
public class QueryExcel {
public static void main(String[] args) {
// 读取Excel文件
String filePath = "E:/jar/userInfo.xls";
try {
// 得到所有数据
List<String> allData = readExcel(filePath);
StringBuilder builder = new StringBuilder();
for(String data : allData) {
builder.append(data).append(",");
}
String result = builder.toString().substring(0, builder.toString().length() - 1);
System.out.println(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 读取指定Sheet也的内容
*
* @param filepath
* filepath 文件全路径
* @param sheetNo
* sheet序号,从0开始,如果读取全文sheetNo设置null
* @throws IOException
* @throws FileNotFoundException
*/
public static List<String> readExcel(String filepath) throws FileNotFoundException, IOException {
List<String> list = new ArrayList<String>();
// 读取文件
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filepath));
HSSFSheet sheet = workbook.getSheetAt(0);
int rowNum = sheet.getPhysicalNumberOfRows();
HSSFRow row;
// 第一行单独处理
int index_phone = -1;
HSSFRow firstRow = sheet.getRow(0);
for (Cell rows : firstRow) {
if (rows != null) {
// 找到电话列
index_phone++;
if ("电话号码".equals(rows.getStringCellValue())) {
System.out.println(index_phone);
break;
}
}
}
for (int i = 1; i < rowNum; i++) {
row = sheet.getRow(i);
if(row != null) {
String phoneNum = row.getCell(index_phone).getStringCellValue();
if(phoneNum != null && !"".equals(phoneNum)) {
list.add(phoneNum);
System.out.println(phoneNum);
}
}
}
return list;
}
}
由于最近很忙,难得整合了,就直接贴源码了。