package per.qy.dexter.fileoperate;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.PictureData;
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;
import org.junit.Test;
public class ExcelTest {
@Test
public void testExcel() {
String path = "D:\\temp\\temp\\test.xls";
// String path = "D:\\temp\\temp\\test.xlsx";
File file = new File(path);
InputStream is = null;
Workbook workbook = null;
try {
is = new FileInputStream(file);
if (path.endsWith(".xls")) {
workbook = new HSSFWorkbook(is);
} else if (path.endsWith(".xlsx")) {
workbook = new XSSFWorkbook(is);
}
if (workbook != null) {
int sheetCount = workbook.getNumberOfSheets();
if (sheetCount > 0) {
// 文本内容
StringBuilder content = new StringBuilder();
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = workbook.getSheetAt(i);
content.append(sheet.getSheetName());
for (int rownum = sheet.getFirstRowNum(); rownum <= sheet.getLastRowNum(); rownum++) {
Row row = sheet.getRow(rownum);
if (row == null || row.getFirstCellNum() < 0) {
break;
}
for (int columnnum = row.getFirstCellNum(); columnnum <= row
.getLastCellNum(); columnnum++) {
String cellValue = getCellValue(row.getCell(columnnum));
content.append(cellValue);
if (content.length() > 500) {// 每500字输出一次
System.out.println(content.toString());
content.delete(0, content.length());
}
}
}
}
if (content.length() > 0) {
System.out.println(content.toString());
}
// 图片内容
List<?> pictures = workbook.getAllPictures();
if (pictures != null && !pictures.isEmpty()) {
for (int i = 0; i < pictures.size(); i++) {
PictureData picture = (PictureData) pictures.get(i);
byte[] data = picture.getData();
FileOutputStream out = new FileOutputStream(
"D:\\temp\\temp\\" + UUID.randomUUID() + ".jpg");
out.write(data);
out.close();
}
}
}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
try {
if (workbook != null) {
workbook.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
}
}
}
private String getCellValue(Cell cell) {
if (cell == null) {
return "";
}
// 都按文本格式读取
cell.setCellType(CellType.STRING);
return cell.getStringCellValue();
}
}
java-poi4.0.1读取excel文本和图片
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近热议的待产孕妇跳楼事件,以及前段时间报道的产妇坐月子中暑死亡事件,都让人不禁想到了老舍1933年出版的短篇讽刺...
- #include//a is dollar amount, b is $20 bills, c is $10 bi...