• java操作Excel之POI(3)


    一、字体处理

     1 /**
     2      * 字体处理
     3      */
     4     public static void main(String[] args) throws Exception {
     5         Workbook wb = new HSSFWorkbook();    
     6         Sheet sheet = wb.createSheet("sheet1");
     7         Row row = sheet.createRow(1);            //创建第2行
     8         
     9         //创建一个字体处理类
    10         Font font = wb.createFont();
    11         font.setFontHeightInPoints((short)24);         //字体大小
    12         font.setFontName("Courier New");
    13         font.setItalic(true);                        //斜体
    14         font.setStrikeout(true);                    //中划线
    15         
    16         CellStyle style = wb.createCellStyle();
    17         style.setFont(font);
    18         
    19         Cell cell = row.createCell(1);
    20         cell.setCellValue("This is test of fonts");
    21         cell.setCellStyle(style);
    22         
    23         FileOutputStream fileOut = new FileOutputStream("E:\工作簿.xls");
    24         wb.write(fileOut);
    25         fileOut.close();
    26     }
    View Code

    二、读取和重写工作簿

     1 /**
     2      * 读取和重写工作簿
     3      * 一般用在:按照一个模板,将这个模板填充数据,再做导出来;
     4      */
     5     public static void main(String[] args) throws Exception {
     6         InputStream is = new FileInputStream("E:\工作簿.xls");
     7         POIFSFileSystem pfs = new POIFSFileSystem(is);
     8         Workbook wb = new HSSFWorkbook(pfs);
     9         Sheet sheet = wb.getSheetAt(0);        //获取第一个sheet页
    10         Row row = sheet.getRow(0);            //获取第一行
    11         
    12         Cell cell = row.getCell(0);            //获取第一个单元格
    13         if(cell == null){
    14             cell = row.createCell(3);
    15         }
    16         
    17         cell.setCellType(Cell.CELL_TYPE_STRING);
    18         cell.setCellValue("测试单元格");
    19         
    20         FileOutputStream fileOut = new FileOutputStream("E:\工作簿.xls");
    21         wb.write(fileOut);
    22         fileOut.close();
    23     }
    View Code

    三、单元格中使用换行

     1 /**
     2      * 单元格中使用换行
     3      */
     4     public static void main(String[] args) throws Exception {
     5         Workbook wb = new HSSFWorkbook();
     6         Sheet sheet = wb.createSheet("sheet1");
     7         Row row = sheet.createRow(2);        //第3行
     8         Cell cell = row.createCell(2);        //第3列
     9         cell.setCellValue("我要换行
    成功了吗?");
    10         
    11         CellStyle cs = wb.createCellStyle();
    12         //设置可以换行
    13         cs.setWrapText(true);
    14         cell.setCellStyle(cs);
    15         
    16         //调整下行的高度
    17         row.setHeightInPoints(2*sheet.getDefaultRowHeightInPoints());
    18         //调整单元格宽度
    19         sheet.autoSizeColumn(2);
    20         
    21         FileOutputStream fileOut = new FileOutputStream("E:\工作簿.xls");
    22         wb.write(fileOut);
    23         fileOut.close();
    24     }
    View Code

    四、创建用户自定义数据格式

     1 /**
     2      * 创建用户自定义数据格式
     3      */
     4     public static void main(String[] args) throws Exception {
     5         Workbook wb = new HSSFWorkbook();
     6         Sheet sheet = wb.createSheet("sheet1");
     7         CellStyle style;
     8         DataFormat format=wb.createDataFormat();
     9         Row row;
    10         Cell cell;
    11         short rowNum=0;
    12         short colNum=0;
    13         
    14         row=sheet.createRow(rowNum++);
    15         cell=row.createCell(colNum);
    16         cell.setCellValue(111111.25);
    17         style=wb.createCellStyle();
    18         style.setDataFormat(format.getFormat("0.0")); //设置数据格式
    19         cell.setCellStyle(style);
    20         
    21         row=sheet.createRow(rowNum++);
    22         cell=row.createCell(colNum);
    23         cell.setCellValue(1111111.25);
    24         style=wb.createCellStyle();
    25         style.setDataFormat(format.getFormat("#,##0.000"));
    26         cell.setCellStyle(style);
    27         
    28         FileOutputStream fileOut = new FileOutputStream("E:\工作簿.xls");
    29         wb.write(fileOut);
    30         fileOut.close();
    31     }
    View Code

  • 相关阅读:
    Orchard1.4发布
    13个MVC的扩展
    不完全接触Node.js
    mac软件
    在Apworks框架中解除NHibernateContext与NHibernateRepository的依赖关系
    mac下我常用的一些软件
    在.NET应用程序中访问Excel的几种方式
    Visual Studio 11 Beta 官方下载地址
    欢迎使用 Windows 8 – Consumer Preview
    PHP学习系列之 环境配置
  • 原文地址:https://www.cnblogs.com/tenWood/p/6422256.html
Copyright © 2020-2023  润新知