zoukankan      html  css  js  c++  java
  • 利用poi插件,把Excel内容读入Java,把Java中的内容输出到Exce

      1 1、首先在Java头引入Poi插件。
      2 
      3 package poiExcel;
      4  
      5 //输入输出到文件必备
      6 import java.io.*;
      7  
      8 import org.apache.poi.hssf.usermodel.*;
      9 import org.apache.poi.hssf.util.*;
     10 import org.apache.poi.poifs.filesystem.*;
     11 2、先是把C:1.xls的内容读取到Java里面来,逐个单元格输出
     12     public static void ExcelRead() throws Exception {
     13         //确定要操作的是c:/1.xls
     14         HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(
     15                 new FileInputStream("c:/1.xls")));
     16         //取第0个单元表
     17         HSSFSheet sheet = workbook.getSheetAt(0);
     18         //sheet.getPhysicalNumberOfRows();求出所有行数
     19         for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
     20             //取一行操作
     21             HSSFRow row = sheet.getRow(i);
     22             //row.getPhysicalNumberOfCells();求出本行的单元格数,也就是列数
     23             for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
     24                 System.out.print(row.getCell(j) + "	");
     25             }
     26             System.out.println();
     27         }
     28     }
     29 3、之后是在Java设置好整个单元格的样式,输出到Excel中。整个过程有点像JavaScript操作HTML的DOM节点。先构造单元格、再通过单元格构造行、最后通过行构造整张表,再把整张表输出的过程。
     30     public static void ExcelWrite() {
     31  
     32         // 创建一个webbook,对应一个Excel文件
     33         HSSFWorkbook workbook = new HSSFWorkbook();
     34         // 在webbook中添加一个Excel单元表sheet,并设置单元表的问题
     35         HSSFSheet sheet = workbook.createSheet("单元表标题");
     36  
     37         // 在sheet中添加第0行,注意老版本poi对Excel的行数列数是有限制
     38         HSSFRow row = sheet.createRow(0);
     39         // 创建一个居中样式
     40         HSSFCellStyle style = workbook.createCellStyle();
     41         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
     42         // 在于这个居中样式的基础上,添加表格边框样式
     43         setBoderStyle(style);
     44         // 创建第0个单元格
     45         HSSFCell cell = row.createCell(0);
     46         // 设置这个单元格的内容为“一”
     47         cell.setCellValue("一");
     48         // 设置这个单元格的格式为上面设置好的居中样式+表格边框样式
     49         cell.setCellStyle(style);
     50         // 同理创建第1个单元格并且设置好样式,下面以此类推
     51         cell = row.createCell(1);
     52         cell.setCellValue("二");
     53         cell.setCellStyle(style);
     54         cell = row.createCell(2);
     55         cell.setCellValue("三");
     56         cell.setCellStyle(style);
     57  
     58         // 创建第1行
     59         row = sheet.createRow(1);
     60         // 清空上面设置好的居中样式+表格边框样式
     61         style = workbook.createCellStyle();
     62         // 设置字体样式
     63         setFontStyle(workbook, style);
     64         cell = row.createCell(0);
     65         cell.setCellValue("111");
     66         cell.setCellStyle(style);
     67         cell = row.createCell(1);
     68         cell.setCellValue("222");
     69         cell.setCellStyle(style);
     70         cell = row.createCell(2);
     71         cell.setCellValue("333");
     72         cell.setCellStyle(style);
     73         // 自动调整列宽
     74         allColumnAutoSize(sheet);
     75  
     76         // 将文件存到指定位置
     77         try {
     78             //false代表覆盖输出
     79             FileOutputStream fileOutputStream = new FileOutputStream(
     80                     "c:/1.xls", false);
     81             workbook.write(fileOutputStream);
     82             //人走带门
     83             fileOutputStream.close();
     84             workbook.close();
     85         } catch (Exception e) {
     86             e.printStackTrace();
     87         }
     88  
     89     }
     90 其中,涉及到设置边框样式的方法如下:
     91     public static void setBoderStyle(HSSFCellStyle style) {
     92         style.setBorderTop((short) 6); // 上边框为双线
     93         style.setBorderRight((short) 3); // 右边框为虚线
     94         style.setBorderBottom((short) 1); // 底边框为单线
     95         style.setBottomBorderColor(HSSFColor.RED.index);// 底边框为红色
     96     }
     97 设置单元格字体的方法如下:
     98     public static void setFontStyle(HSSFWorkbook workbook, HSSFCellStyle style) {
     99         HSSFFont font = workbook.createFont();// 要设置字体样式先要创建字体
    100         font.setFontHeightInPoints((short) 16);// 字号
    101         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
    102         font.setItalic(true);// 斜体
    103         font.setColor(HSSFColor.RED.index);// 字体颜色是红色
    104         style.setFont(font); // 把这个设置好的字体样色压入样式
    105     }
    106 自动调整列宽的方法如下:
    107 
    108     public static void allColumnAutoSize(HSSFSheet sheet) {
    109         // 遍历所有单元格,把单元格皆设置为最优列宽。
    110         for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
    111             HSSFRow row = sheet.getRow(i);
    112             for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
    113                 sheet.autoSizeColumn(j);
    114             }
    115         }
    116     }
    117 
    118 
    119 4.总结
    120 
    121 最后整个Java文件如下,主函数就两个方法,一个EXCEL到JAVA,一个JAVA到EXCEL。实际应用这个插件,创建单元格完全可以利用for与while等循环,从一个ArrayList读取数据,不停地填充单元格。
    122 
    123 package poiExcel;
    124  
    125 //输入输出到文件必备
    126 import java.io.*;
    127  
    128 import org.apache.poi.hssf.usermodel.*;
    129 import org.apache.poi.hssf.util.*;
    130 import org.apache.poi.poifs.filesystem.*;
    131  
    132 public class poiTest {
    133  
    134     public static void ExcelRead() throws Exception {
    135         //确定要操作的是c:/1.xls
    136         HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(
    137                 new FileInputStream("c:/1.xls")));
    138         //取第0个单元表
    139         HSSFSheet sheet = workbook.getSheetAt(0);
    140         //sheet.getPhysicalNumberOfRows();求出所有行数
    141         for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
    142             //取一行操作
    143             HSSFRow row = sheet.getRow(i);
    144             //row.getPhysicalNumberOfCells();求出本行的单元格数,也就是列数
    145             for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
    146                 System.out.print(row.getCell(j) + "	");
    147             }
    148             System.out.println();
    149         }
    150     }
    151  
    152     public static void setBoderStyle(HSSFCellStyle style) {
    153         style.setBorderTop((short) 6); // 上边框为双线
    154         style.setBorderRight((short) 3); // 右边框为虚线
    155         style.setBorderBottom((short) 1); // 底边框为单线
    156         style.setBottomBorderColor(HSSFColor.RED.index);// 底边框为红色
    157     }
    158  
    159     public static void setFontStyle(HSSFWorkbook workbook, HSSFCellStyle style) {
    160         HSSFFont font = workbook.createFont();// 要设置字体样式先要创建字体
    161         font.setFontHeightInPoints((short) 16);// 字号
    162         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
    163         font.setItalic(true);// 斜体
    164         font.setColor(HSSFColor.RED.index);// 字体颜色是红色
    165         style.setFont(font); // 把这个设置好的字体样色压入样式
    166     }
    167  
    168     public static void allColumnAutoSize(HSSFSheet sheet) {
    169         // 遍历所有单元格,把单元格皆设置为最优列宽。
    170         for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
    171             HSSFRow row = sheet.getRow(i);
    172             for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
    173                 sheet.autoSizeColumn(j);
    174             }
    175         }
    176     }
    177  
    178     public static void ExcelWrite() {
    179  
    180         // 创建一个webbook,对应一个Excel文件
    181         HSSFWorkbook workbook = new HSSFWorkbook();
    182         // 在webbook中添加一个Excel单元表sheet,并设置单元表的问题
    183         HSSFSheet sheet = workbook.createSheet("单元表标题");
    184  
    185         // 在sheet中添加第0行,注意老版本poi对Excel的行数列数是有限制
    186         HSSFRow row = sheet.createRow(0);
    187         // 创建一个居中样式
    188         HSSFCellStyle style = workbook.createCellStyle();
    189         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    190         // 在于这个居中样式的基础上,添加表格边框样式
    191         setBoderStyle(style);
    192         // 创建第0个单元格
    193         HSSFCell cell = row.createCell(0);
    194         // 设置这个单元格的内容为“一”
    195         cell.setCellValue("一");
    196         // 设置这个单元格的格式为上面设置好的居中样式+表格边框样式
    197         cell.setCellStyle(style);
    198         // 同理创建第1个单元格并且设置好样式,下面以此类推
    199         cell = row.createCell(1);
    200         cell.setCellValue("二");
    201         cell.setCellStyle(style);
    202         cell = row.createCell(2);
    203         cell.setCellValue("三");
    204         cell.setCellStyle(style);
    205  
    206         // 创建第1行
    207         row = sheet.createRow(1);
    208         // 清空上面设置好的居中样式+表格边框样式
    209         style = workbook.createCellStyle();
    210         // 设置字体样式
    211         setFontStyle(workbook, style);
    212         cell = row.createCell(0);
    213         cell.setCellValue("111");
    214         cell.setCellStyle(style);
    215         cell = row.createCell(1);
    216         cell.setCellValue("222");
    217         cell.setCellStyle(style);
    218         cell = row.createCell(2);
    219         cell.setCellValue("333");
    220         cell.setCellStyle(style);
    221         // 自动调整列宽
    222         allColumnAutoSize(sheet);
    223  
    224         // 将文件存到指定位置
    225         try {
    226             //false代表覆盖输出
    227             FileOutputStream fileOutputStream = new FileOutputStream(
    228                     "c:/1.xls", false);
    229             workbook.write(fileOutputStream);
    230             //人走带门
    231             fileOutputStream.close();
    232             workbook.close();
    233         } catch (Exception e) {
    234             e.printStackTrace();
    235         }
    236  
    237     }
    238  
    239     public static void main(String[] args) throws Exception {
    240         ExcelRead();
    241         ExcelWrite();
    242     }
    243  
    244 }
  • 相关阅读:
    Mysql优化(1) 存储引擎、数据类型、字符集
    Mysql常用命令(5) 增加表的字段、修改表名、备份数据库
    Mysql常用命令(4) 插入、查询、删除、修改数据
    Navicat Premium v12.1.28 中文最新破解版(附:激活工具)
    MySQL命令:创建数据库、插入数据
    mysql初始化密码错误
    mysql第一次安装成功后初始化密码操作步骤
    MYSQL安装出现问题(服务无法启动,The service already exists)
    练习4-7 求e的近似值 (15 分)
    习题3-2 高速公路超速处罚 (15 分)
  • 原文地址:https://www.cnblogs.com/jiayiblog/p/11466645.html
Copyright © 2011-2022 走看看