java xlsx读写_Java读写Excel
Java讀寫Excel
工作中經(jīng)常需要對(duì)Excel進(jìn)行讀寫操作,java操作excel文件比較流行的是apache poi包,excel分為xls(2003)和xlsx(2007)兩種格式,操作這兩種格式的excel需要不同的poi包。
xls格式
org.apache.poi
poi
3.11-beta1
xlsx格式
org.apache.poi
poi-ooxml
3.11-beta1
讀xls
File file = new File("src/test/resources/test.xls");
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(poifsFileSystem);
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
int rowstart = hssfSheet.getFirstRowNum();
int rowEnd = hssfSheet.getLastRowNum();
for(int i=rowstart;i<=rowEnd;i++)
{
HSSFRow row = hssfSheet.getRow(i);
if(null == row) continue;
int cellStart = row.getFirstCellNum();
int cellEnd = row.getLastCellNum();
for(int k=cellStart;k<=cellEnd;k++)
{
HSSFCell cell = row.getCell(k);
if(null==cell) continue;
System.out.print("" + k + " ");
//System.out.print("type:"+cell.getCellType());
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 數(shù)字
System.out.print(cell.getNumericCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + " ");
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知類型 ");
break;
}
}
System.out.print("\n");
}
讀xlsx
File file = new File("src/test/resources/test.xlsx");
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file);
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
int rowstart = xssfSheet.getFirstRowNum();
int rowEnd = xssfSheet.getLastRowNum();
for(int i=rowstart;i<=rowEnd;i++)
{
XSSFRow row = xssfSheet.getRow(i);
if(null == row) continue;
int cellStart = row.getFirstCellNum();
int cellEnd = row.getLastCellNum();
for(int k=cellStart;k<=cellEnd;k++)
{
XSSFCell cell = row.getCell(k);
if(null==cell) continue;
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 數(shù)字
System.out.print(cell.getNumericCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue()
+ " ");
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + " ");
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知類型 ");
break;
}
}
System.out.print("\n");
}
寫xls
HSSFWorkbook workbook = null;
workbook = new HSSFWorkbook();
//獲取參數(shù)個(gè)數(shù)作為excel列數(shù)
int columeCount = 6;
//獲取List size作為excel行數(shù)
int rowCount = 20;
HSSFSheet sheet = workbook.createSheet("sheet name");
//創(chuàng)建第一欄
HSSFRow headRow = sheet.createRow(0);
String[] titleArray = {"id", "name", "age", "email", "address", "phone"};
for(int m=0;m<=columeCount-1;m++)
{
HSSFCell cell = headRow.createCell(m);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
sheet.setColumnWidth(m, 6000);
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
short color = HSSFColor.RED.index;
font.setColor(color);
style.setFont(font);
//填寫數(shù)據(jù)
cell.setCellStyle(style);
cell.setCellValue(titleArray[m]);
}
int index = 0;
//寫入數(shù)據(jù)
for(RowEntity entity : pRowEntityList)
{
//logger.info("寫入一行");
HSSFRow row = sheet.createRow(index+1);
for(int n=0;n<=columeCount-1;n++)
row.createCell(n);
row.getCell(0).setCellValue(entity.getId());
row.getCell(1).setCellValue(entity.getName());
row.getCell(2).setCellValue(entity.getAge());
row.getCell(3).setCellValue(entity.getEmail());
row.getCell(4).setCellValue(entity.getAddress());
row.getCell(5).setCellValue(entity.getPhone());
index++;
}
//寫到磁盤上
try {
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
workbook.write(fileOutputStream);
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
寫xlsx
和寫xls類似,使用2007對(duì)應(yīng)的對(duì)象即可。
總結(jié)
以上是生活随笔為你收集整理的java xlsx读写_Java读写Excel的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java transient_【转】Ja
- 下一篇: mysql数据库内NOT NULL_浅谈