`

POI基本操作Excel

    博客分类:
  • java
阅读更多

      Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。

所需jar:poi-3.10.1-20140818.jar

1、创建Excel表

@Test
public void testCreateWorkbook(){
	//创建一个Excel表
	Workbook wb = new HSSFWorkbook();
	
	File file = new File("D:"+File.separator+"first.xls");
	FileOutputStream out = null;
	try {
		out = new FileOutputStream(file);
		wb.write(out);
		out.flush();
		out.close();
		System.out.println("success");
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

 2、创建工作区间  

@Test
public void testCreateSheet(){
	Workbook wb = new HSSFWorkbook();
	//创建一个工作区间
	Sheet sheet = wb.createSheet("one sheet");
	
	File file = new File("D:"+File.separator+"first.xls");
	FileOutputStream out = null;
	try {
		out = new FileOutputStream(file);
		wb.write(out);
		out.flush();
		out.close();
		System.out.println("success");
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

  3、创建单元格

@Test
public void testCreateCells(){
	Workbook wb = new HSSFWorkbook();
	Sheet sheet = wb.createSheet("one sheet");
	CreationHelper createHelper = wb.getCreationHelper();
	
	//创建第一行
	Row row = sheet.createRow(0);
	
	//创建单元格
	Cell cell = row.createCell(0);
	cell.setCellValue(1);
	
	//或者一下方式创建单元格
	row.createCell(1).setCellValue(1.2);
	row.createCell(2).setCellValue(createHelper.createRichTextString("This is a String"));
	row.createCell(3).setCellValue(true);
	
	File file = new File("D:"+File.separator+"first.xls");
	FileOutputStream out = null;
	try {
		out = new FileOutputStream(file);
		wb.write(out);
		out.flush();
		out.close();
		System.out.println("success");
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

 4、创建其它格式单元格

@Test
public void testCreateOtherCells(){
	Workbook wb = new HSSFWorkbook();
	Sheet sheet = wb.createSheet("one sheet");
	
	//创建第一行
	Row row = sheet.createRow(0);
	
	//创建单元格
	row.createCell(0).setCellValue(1.1);
    row.createCell(1).setCellValue(new Date());
    row.createCell(2).setCellValue(Calendar.getInstance());
    row.createCell(3).setCellValue("a string");
    row.createCell(4).setCellValue(true);
    row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
	
	
	File file = new File("D:"+File.separator+"first.xls");
	FileOutputStream out = null;
	try {
		out = new FileOutputStream(file);
		wb.write(out);
		out.flush();
		out.close();
		System.out.println("success");
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

5、单元格对齐方式

@Test
public void testAlignmentOptions(){
	Workbook wb = new HSSFWorkbook();
	Sheet sheet = wb.createSheet();
	
	//创建第一行
	Row row = sheet.createRow(2);
	row.setHeightInPoints(30);
	
	//创建单元格
	TestUtil.createCell(wb, row, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
	TestUtil.createCell(wb, row, 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
	TestUtil.createCell(wb, row, 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
	TestUtil.createCell(wb, row, 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
	TestUtil.createCell(wb, row, 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
	TestUtil.createCell(wb, row, 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
	TestUtil.createCell(wb, row, 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);
		
	File file = new File("D:"+File.separator+"first.xls");
	FileOutputStream out = null;
	try {
		out = new FileOutputStream(file);
		wb.write(out);
		out.flush();
		out.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

public static void createCell(Workbook wb, Row row, int column, short halign, short valign) {
    Cell cell = row.createCell(column);
    cell.setCellValue("Align It");
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(halign);
    cellStyle.setVerticalAlignment(valign);
    cell.setCellStyle(cellStyle);
}

 6、单元格加粗

@Test
public void testBorder(){
	Workbook wb = new HSSFWorkbook();
	Sheet sheet = wb.createSheet();
	//创建行
	Row row = sheet.createRow(1);
	//创建单元格
	Cell cell = row.createCell(1);
	cell.setCellValue(4);
	//创建单元格样式
	CellStyle style = wb.createCellStyle();
	style.setBorderBottom(CellStyle.BORDER_THIN);	//边框类型
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());	//边框颜色
    style.setBorderLeft(CellStyle.BORDER_THIN);
    style.setLeftBorderColor(IndexedColors.GREEN.getIndex());
    style.setBorderRight(CellStyle.BORDER_THIN);
    style.setRightBorderColor(IndexedColors.BLUE.getIndex());
    style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
    style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    cell.setCellStyle(style);
	
	File file = new File("D:"+File.separator+"first.xls");
	FileOutputStream out = null;
	try {
		out = new FileOutputStream(file);
		wb.write(out);
		out.flush();
		out.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

 7、单元格加颜色

@Test
public void testColor(){
	Workbook wb = new HSSFWorkbook();
	Sheet sheet = wb.createSheet();
	
	//创建行
	Row row = sheet.createRow(1);
	
	//创建字体样式
	Font font = wb.createFont();
	font.setFontName("宋体");	//字体类型
	font.setFontHeightInPoints((short)12);	//字体大小
	font.setItalic(true);	//倾斜
	font.setBoldweight(Font.BOLDWEIGHT_BOLD);	//加粗
	font.setColor(HSSFColor.RED.index);		//红色

	//创建单元格样式
	CellStyle style = wb.createCellStyle();
	style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());	//背景颜色 
	style.setFillPattern(CellStyle.SOLID_FOREGROUND);	//全部填充  (填充类型)
	style.setFont(font);	//关联字体
	
	//创建单元格
	Cell cell = row.createCell(1);
	cell.setCellValue("呵呵");
	cell.setCellStyle(style);
	
	File file = new File("D:"+File.separator+"first.xls");
	FileOutputStream out = null;
	try {
		out = new FileOutputStream(file);
		wb.write(out);
		out.flush();
		out.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	
}

 8、合并单元格

@Test
public void testMergingCell(){
	Workbook wb = new HSSFWorkbook();
	Sheet sheet = wb.createSheet();
	
	//创建行
	Row row = sheet.createRow(1);
	
	Cell cell = row.createCell(1);
	cell.setCellValue("hello");
	
	//合并单元格(开始行数,结束行数,开始列数,结束列数)注意:都是从0开始的
	sheet.addMergedRegion(new CellRangeAddress(1,1,1,2));
	
	
	File file = new File("D:"+File.separator+"first.xls");
	FileOutputStream out = null;
	try {
		out = new FileOutputStream(file);
		wb.write(out);
		out.flush();
		out.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

 9、迭代读入Excel中的单元格内容(解决int与String的转换问题)

@Test
public void testIterateRowsAndCells(){
	Workbook wb = null;
	try {
		wb = new HSSFWorkbook(new FileInputStream(new File("F:\\test.xls")));
		Sheet sheet = wb.getSheetAt(0);
	    for (Row row : sheet) {
	      for (Cell cell : row) {
	    	  CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
	            System.out.print(cellRef.formatAsString());
	            System.out.print(" - ");

	            switch (cell.getCellType()) {
	                case Cell.CELL_TYPE_STRING:
	                    System.out.println(cell.getRichStringCellValue().getString());
	                    break;
	                case Cell.CELL_TYPE_NUMERIC:
	                    if (DateUtil.isCellDateFormatted(cell)) {
	                        System.out.println(cell.getDateCellValue());
	                    } else {
	                    	cell.setCellType(Cell.CELL_TYPE_STRING);
                            String temp = cell.getStringCellValue();
                            String str = "";
                            if (temp.indexOf(".") > -1) {
                                str = String.valueOf(new Double(temp))
                                        .trim();
                            } else {
                                str = temp.trim();
                            }
	                        System.out.println(str);
	                    }
	                    break;
	                case Cell.CELL_TYPE_BOOLEAN:
	                    System.out.println(cell.getBooleanCellValue());
	                    break;
	                case Cell.CELL_TYPE_FORMULA:
	                    System.out.println(cell.getCellFormula());
	                    break;
	                case Cell.CELL_TYPE_BLANK:
	                	System.out.println("\"\"");
	                	break;
	                default:
	                    System.out.println("未知类型");
	            }
	      }
	    }
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

 

 

分享到:
评论

相关推荐

    java_poi操作excel

    java通过poi操作excel的基本例子. 单元格基本操作 poi画线 矩形 圆形 excel表的操作 行列操作

    poi操作Excel表格基本操作

    poi操作Excel表格基本操作

    POI基本操作

    POI包对Excel的基本操作使用。

    POI实战-java开发excel详解

    1.2 POI基本类 5 1.3 POI简单读取Excel数据 5 1.4 POI简单写出Excel 9 2.复杂读取 16 2.1 单元格各类型数据读取 16 2.1.1 基本类型 16 2.1.2 日期类型 18 2.2 自定义类型 21 3.复杂写入 22 3.1 复杂写入 22 3.2 ...

    POI操作excel2003与2007

    通过实例代码,帮助大家掌握POI操作excel2003,2007调用的方法名不同,但基本类似。

    通用的POI导入Excel解决方案-ExcelUtilTest

    在项目中,经常免不了要导入、导出Excel,导出Excel稍微简单点,制作一张模板,然后将查询之后的数据写入到模板中即可,导出程序可以做到一次写好,基本上就可以通用。 但导入Excel相对麻烦点,一般情况下,导入的...

    Java调用POI操作excel详解

    Java调用POI操作excel,此篇 POI 讲解相对基础,都是平时我们在开发中用到的功能。主要包括 Excel的读取、写入,各种数据格式处理、单元格合并、注释、下拉列表及单元格的边框、背景色、宽高度调整等。

    java用poi导入,到处excel操作

    用java开发,根据poi操作excel的导入,导出功能,基本上能满足业务的80%需求操作

    POI实战 .pdf

    1.2 POI基本类 5 1.3 POI简单读取Excel数据 5 1.4 POI简单写出Excel 9 2.复杂读取 16 2.1 单元格各类型数据读取 16 2.1.1 基本类型 16 2.1.2 日期类型 18 2.2 自定义类型 21 3.复杂写入 22 3.1 复杂写入 22 3.2 ...

    java POI导出excel

    这个是利用POI导出excel的源码,需要导入poi包,然后把jsp和action的代码拷贝进去就可以直接运行,很基础的代码,很适合初学者学习。 POI可以自己定义导出excel格式的数据,实例就是这样弄的,希望对初学者有帮助。

    poiExcel简单操作

    poiExcel简单操作,这里面主要举了一个例子,这个例子是简单的讲述如何使用poi获取Excle里面的值的方式,很简单,但是也是最基础的东西。

    通用的POI导入Excel解决方案

    然后在这个基础上,我们可以对数据进行一些操作,如:数据校验,插入数据。。。 其中数据校验时,如果数据不合法,给用户的提示信息可以精确指定某个单元格的数据不合法。(如:提示信息为:A3单元格数据不合法!) ...

    POI 官方API大全及基本操作实例(含jar包)

    POI 官方API大全及基本操作实例(含jar包),通过实例和查看API,你能够很快掌握基本的excel操作。

    POI操作WORD 官方测试案例 DEMO.zip

    Apache POI 提供 Java 操作 Excel 解决方案(适用于 Excel97-2008)。 2 Apache POI的组件 Apache POI包含用于MS-Office的所有OLE2复合文档的类和方法。 此API的组件列表如下: POIFS:此组件是所有其他POI元素的...

    POI3.5_HSSF_和XSSF_Excel操作快速入门手册.pdf

    POI3.5_HSSF_和XSSF_Excel操作快速入门手册.pdf

    个人java工具集,含poi-excel的模板导出

    个人java工具集,含有String list map等,basedao(jdbc的基础操作),excel-poi的模板工具(在模板excel中打标机${key}或 ${list.key}) 直接替换指定标记,保留字体格式,内有excel模板使用说明

    java操作EXCEL,jar包,含实例教程,poi-3.17

    POIFS (较差混淆技术实现文件系统) : 此组件是所有其他POI元件的基本因素。它被用来明确地读取不同的文件。 HSSF (可怕的电子表格格式) : 它被用来读取和写入MS-Excel文件的xls格式。 XSSF (XML格式) : 它是用于MS...

    POI操作Excel文档-基础篇.pdf

    。。。

    poi基础教程

    poi的基础使用教程,poi操作excel 上传,下载,单元格的操作

    springMVC poi解析ajax上传excel文件,返回json对象\list数组

    解析通过MutilpartFile导入的Excel并解析里面数据,先判断文件的类型(excel处理有两种此处为两种通用)是.xls/.xlsx,采用Apache的POI的API来操作Excel,读取内容后保存到List中,再将List转Json(使用Linked,增删快...

Global site tag (gtag.js) - Google Analytics