Poi对excel的基本操作
1.创建简单excel
    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet s1=wb.createSheet("第一个sheet页");//创建Sheet页
        Row row=s1.createRow(0);//创建数据行
        Cell cell=row.createCell(0);//创建单元格
        cell.setCellValue(1);
        row.createCell(1).setCellValue(1.2);//单元格可写入不同格式的数据
        row.createCell(2).setCellValue("啦啦");
        row.createCell(3).setCellValue(false);
        FileOutputStream file=new FileOutputStream("e://poi_createCell.xls");
        wb.write(file);
        file.close();
        wb.close();
    }
2.创建指定日期格式
    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("diyigeSheet页");
        Row row=sheet.createRow(0);
        Cell cell=row.createCell(0);
        cell.setCellValue(new Date());//默认日期类型 42796.89489
        CreationHelper creationHelper=wb.getCreationHelper();
        CellStyle cellStyle=wb.createCellStyle();
        cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yy-MM-dd:hh ss"));
        cell=row.createCell(1);//创建指定日期类型数据 17-03-02:21 39
        cell.setCellValue(new Date());
        cell.setCellStyle(cellStyle);
        cell=row.createCell(2);//创建指定日期类型数据(第二种方式) 17-03-02:21 39
        cell.setCellValue(Calendar.getInstance());
        cell.setCellStyle(cellStyle);
        FileOutputStream fs=new FileOutputStream("e://poi_createData.xls");
        wb.write(fs);
        fs.close();
        wb.close();
    }
3.读取excel内容,以文本格式展示
    public static void main(String[] args) throws Exception {
        InputStream is=new FileInputStream("e:\\poi_read.xls");
        POIFSFileSystem fs=new POIFSFileSystem(is);
        HSSFWorkbook wb=new HSSFWorkbook(fs);
        @SuppressWarnings("resource")
        ExcelExtractor excelExtractor=new ExcelExtractor(wb);
        excelExtractor.setIncludeSheetNames(false);//控制是否读取sheet页名字
        System.out.println(excelExtractor.getText());
    }
4.设置excel中单元格内容样式
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException {
        Workbook wb=new HSSFWorkbook();
        Sheet s1=wb.createSheet("第一个sheet页");
        Row row=s1.createRow(0);
        row.setHeightInPoints(30);
        createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);//内容底部居中
        createCell(wb,row,(short)1,HSSFCellStyle.ALIGN_FILL,HSSFCellStyle.VERTICAL_CENTER);//内容靠左垂直居中
        createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);//内容左上
        createCell(wb,row,(short)3,HSSFCellStyle.ALIGN_RIGHT,HSSFCellStyle.VERTICAL_TOP);//内容右上
        FileOutputStream fileOut=new FileOutputStream("e:\\demo.xls");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }
    //设置单元格样式
    private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
        Cell cell=row.createCell(column);//创建单元格
        cell.setCellValue(new HSSFRichTextString("Align It"));//设置值
        CellStyle cellStyle=wb.createCellStyle();//创建单元格样式
        cellStyle.setAlignment(halign);//设置单元格水平方向对齐方式
        cellStyle.setVerticalAlignment(valign);//设置单元格垂直方向对齐方式
        cell.setCellStyle(cellStyle);//设置单元格样式
    }
5.设置excel中单元格样式
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("sheet1");
        Row row=sheet.createRow(1);
        Cell cell=row.createCell(1);
        cell.setCellValue(4);
        CellStyle cellStyle=wb.createCellStyle();
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);//底部边框
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());//底部边框颜色
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);//左边边框
        cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex());//左边边框颜色
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);//右边边框
        cellStyle.setRightBorderColor(IndexedColors.RED.getIndex());//右边边框颜色
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);//上边边框
        cellStyle.setTopBorderColor(IndexedColors.BLUE.getIndex());//上边边框颜色
        cell.setCellStyle(cellStyle);
        FileOutputStream fileOut=new FileOutputStream("e:\\createExcelDemo08.xls");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }
6.设置excel中单元格前景色和背景色
    public static void main(String[] args) throws Exception {
        HSSFWorkbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("第一个sheet");
        Row row=sheet.createRow(1);
        Cell cell=row.createCell(1);
        cell.setCellValue("sjdsk");
        CellStyle cellStyle=wb.createCellStyle();
        cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());//设置背景色
        cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
        cell.setCellStyle(cellStyle);
        Cell cell2=row.createCell(2);
        cell2.setCellValue("sjdsk");
        CellStyle cellStyle2=wb.createCellStyle();
        cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex());//设置前景色
        cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell2.setCellStyle(cellStyle2);
        FileOutputStream fileOut=new FileOutputStream("e://createDemo10a.xls");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }
7.设置excel中合并单元格
    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();//定义一个新的工作簿
        Sheet sheet=wb.createSheet("第一个sheet页");
        Row row=sheet.createRow(1);
        Cell cell=row.createCell(1);
        cell.setCellValue("单元格合并测试");
        sheet.addMergedRegion(new CellRangeAddress(
            1,//起始行
            2,//结束行
            1,//起始列
            2//结束列
            ));
        FileOutputStream file=new FileOutputStream("e:\\createExcelDemo11a.xls");
        wb.write(file);
        file.close();
        wb.close();
    }
8.设置excel文字样式
    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("第一个sheet页啦");
        Row row=sheet.createRow(1);
        //创建一个字体处理类
        Font font=wb.createFont();
        font.setFontHeightInPoints((short)24);//设置字体大小
        font.setItalic(true);//设置斜体
        font.setStrikeout(true);//设置文字是否划线
        CellStyle style=wb.createCellStyle();
        style.setFont(font);
        Cell cell=row.createCell(1);
        cell.setCellValue("there are build a font");
        cell.setCellStyle(style);
        FileOutputStream file=new FileOutputStream("e:\\createExcelDemo12.xls");
        wb.write(file);
        file.close();
        wb.close();
    }
9.设置添加数据到已有的单元格
    public static void main(String[] args) throws Exception {
        InputStream input=new FileInputStream("e:\\createExcelDemo12.xls");
        POIFSFileSystem fs=new POIFSFileSystem(input);
        Workbook wb=new HSSFWorkbook(fs);
        Sheet sheet=wb.getSheetAt(0);
        Row row=sheet.getRow(1);
        Cell cell=row.getCell(1);
        if(cell==null){
            cell=row.createCell(3);
        }
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue("测试POI添加数据到已有");
        FileOutputStream fos=new FileOutputStream("e:\\createExcelDemo12.xls");
        wb.write(fos);
        fos.close();
        wb.close();
    }
10.设置excel单元格中可换行
    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        Sheet sheet=wb.createSheet("第一个sheet页");
        Row row=sheet.createRow(0);
        Cell cell=row.createCell(0);
        cell.setCellValue("换个行啦\n 换了吗???");
        CellStyle cs=wb.createCellStyle();
        //设置可以换行
        cs.setWrapText(true);
        cell.setCellStyle(cs);
        //调整下行的高度
        row.setHeightInPoints(2*sheet.getDefaultRowHeightInPoints());
        //调整单元格宽度
        sheet.autoSizeColumn(2);
        FileOutputStream fileOut=new FileOutputStream("e:\\createExceDemo14.xls");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
    }
11.设置excel中单元格数据精度
    public static void main(String[] args) throws Exception {
        Workbook wb=new HSSFWorkbook();
        FileOutputStream file=new FileOutputStream("e:\\createExcelPOI_DEMO15.xls");
        Sheet sheet=wb.createSheet();
        CellStyle style;
        DataFormat format=wb.createDataFormat();
        Row row;
        Cell cell;
        short rowNum=0;
        short cellNum=0;
        row=sheet.createRow(rowNum++);
        cell=row.createCell(cellNum++);
        cell.setCellValue(12.908);
        style=wb.createCellStyle();
        style.setDataFormat(format.getFormat("0.0"));
        cell.setCellStyle(style);//输出12.9
        row=sheet.createRow(rowNum++);
        cell=row.createCell(cellNum);
        cell.setCellValue(111111111111111.90899);
        style=wb.createCellStyle();
        style.setDataFormat(format.getFormat("#,##0,000"));
        cell.setCellStyle(style);//输出111,111,111,111,112
        wb.write(file);
        file.close();
        wb.close();
    }
Poi对excel的基本操作的更多相关文章
- POI操作Excel
		
POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...
 - poi对excel的基本读写操作
		
最近简单的弄了下poi对excel的应用,为方便自己以后的使用就把一些基本操作记录下来,其他更复杂的操作可以等以后有需求的时候再来深入了解一番! 写操作: /** * * 层次结构就是workbook ...
 - 使用POI导出Excel(二)-利用模板
		
一.基本操作见: 使用POI导出Excel 二.本次功能需求 给了一个模板,里面有6个sheet页,每页里面都需要填充相应的数据.如图: 三.需求分析 1.分了6个sheet页,每页的数据都不一样,首 ...
 - POI对Excel自定义日期格式的读取
		
用POI读取Excel数据:(版本号:POI3.7) 1.读取Excel private List<String[]> rosolveFile(InputStream is, String ...
 - poi导出excel
		
Java使用poi组件导出excel报表,能导出excel报表的还可以使用jxl组件,但jxl想对于poi功能有限,jxl应该不能载excel插入浮动层图片,poi能很好的实现输出excel各种功能, ...
 - JAVA的POI操作Excel
		
1.1Excel简介 一个excel文件就是一个工作簿workbook,一个工作簿中可以创建多张工作表sheet,而一个工作表中包含多个单元格Cell,这些单元格都是由列(Column)行(Row)组 ...
 - POI导出excel日期格式
		
参考帖子: [1]http://www.ithao123.cn/content-2028409.html [2]http://javacrazyer.iteye.com/blog/894850 再读本 ...
 - 使用jxl,poi读取excel文件
		
作用:在java后台添加一个方法,读取导入的excel内容,根据需要返回相应的sql语句,以完成对临时表的插入操作. 使用jxl读取excel文件 package com.sixthf.bi.sapp ...
 - POI读取Excel内容格式化
		
在用POI读取Excel内容时,经常会遇到数据格式化的问题. 比如:数字12365会变为12365.0;字符串数字123也会变为123.0,甚至会被变为科学计数法.另外日期格式化也是一个头疼的问题.其 ...
 
随机推荐
- ListBox item Sort
			
将Rss内容读取到Listbox control中, 然后实现按照标题或发布日期进行排序. private void ListItemSort(string type) { if (type == & ...
 - magento首页点击任何产品都出现404错误的问题方法
			
很简单,只要在其他项目上的根目录上复制一个.htaccess文件到项目跟目录下就可以 了,其实就是是设置伪静态:
 - CodeForces - 547D:  Mike and Fish (转化为欧拉回路)(优化dfs稠密图)(定向问题)
			
As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange ...
 - ThinkPHP3.2.3整合smarty模板(三)
			
在smarty模板中使用thinkphp框架的U方法时要主要的问题: 1.不能直接使用{:U('Index/index')}: 2.正确的使用方法为:<!--{U("Login/log ...
 - PAT 团体程序设计天梯赛 L1-046 整除光棍(模拟除法)
			
L1-046. 整除光棍 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 翁恺 这里所谓的"光棍",并不是指单身汪 ...
 - mysql完整版
			
mysql: dbs 数据库系统 bdms 数据库管理系统 bda 数据库管理员 db 数据库 dba通过dbms来操作db! 关系型数据库和非关系型数据库 登录mysql mysql -h主机地址 ...
 - java语法基础练习
			
1.阅读示例: EnumTest.java,并运行.分析结果 代码: public class EnumTest { public static void main(String[] args) { ...
 - C# 处理DateTime算法,取某月第1天及最后一天
			
代码如下所示: /// <summary> /// 取得某月的第一天 /// </summary> /// <param name="datetime" ...
 - Crazy Computer
			
ZS the Coder is coding on a crazy computer. If you don't type in a word for a cconsecutive seconds, ...
 - BZOJ4713 迷失的字符串
			
分析 首先考虑只有一个串时的做法,可以进行背包dp,记\(f(i,j)\)表示从\(i\)的子树中某点出发到\(i\)能否匹配字符串的\(1 \dots j\)位且\(i\)与\(j\)匹配.同时记\ ...