Apache POI和excel操作
2021-05-12 18:28
                         标签:png   visio   bsp   resource   exp   使用   temp   rac   boolean    描述 POI结构: 测试类   异常处理 java.lang.IllegalStateException: Cannot get a text value from a numeric cell     输出结果: 张三   一、当我们遇到用“数字”表示的【文本型】内容时,怎么处理 方法一:在输入这串“数字”之前,先输入一个英文符号【‘】(也就是输入法为英文状态时,输入一个单引号)。 比如你需要输入一个信息“0023”,如果你不输入【’】,直接输入“0023”,回车后单元格内的信息将显示的是“23”。 方法二:先选择这个单元格或这部分单元格,鼠标右击选中部分,在弹出 的菜单中选【设置单元格格式】,再在弹出的窗口中依次选择【数字】-【文本】-【确定】,最后输入”0023“。 经过这样的操作,你会发现,在单元格的左上角会有一个”小的绿色的三角“符号,表示”这个单元格里的内容被强制转换成了文本型内容“。 特别适用于:保存身份证号信息、保存0开头的数字信息。 POI操作Excel表格封装了几个核心对象: XSSFWorkbook:工作簿     XSSFSheet:工作表       Row:行          Cell:单元格 上面案例是通过遍历工作表获得行,遍历行获得单元格,最终获取单元格中的值。 还有一种方式就是获取工作表最后一个行号,从而根据行号获得行对象,通过行获取最后一个单元格索引,从而根据单元格索引获取每行的一个单元格对象,代码如下 写数据进入表中 结果:       复杂execl案例 前端遍历的语句   Apache POI和excel操作 标签:png   visio   bsp   resource   exp   使用   temp   rac   boolean    原文地址:https://www.cnblogs.com/fdy-study-consist/p/12005011.html
package com.study.test;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.IOException;
public class PoiTest01 {
    @Test
    public void test01() throws IOException {
        //1 创建工作簿对象
        XSSFWorkbook workbook = new XSSFWorkbook("G:/hello.xlsx");
        //2 获得工作表对象
        XSSFSheet sheet = workbook.getSheetAt(0);
        //3 遍历工作表 获得行对象
        for (Row row : sheet) {
            //4 遍历行对象 获得列对象
            for (Cell cell : row) {
                //5 获得列里面的内容
                System.out.println(cell.getStringCellValue());
            }
            System.out.println("------------");
        }
        //6.关闭
        workbook.close();
    }
}

李四
------------
45
82
------------
89
23
------------  // 1.创建工作簿对象
        XSSFWorkbook workbook = new XSSFWorkbook("G:/hello.xlsx");
        // 2.获得工作表对象
        XSSFSheet sheet = workbook.getSheetAt(0);// Excel默认有三个表对象
        // 3.获得最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
        // 4.遍历行
        for(int i =0;i){
            XSSFRow row = sheet.getRow(i);
            // 5.获得最后一列的列号
            short lastCellNum = row.getLastCellNum();
            // 6.遍历列
            for(int j = 0;j
  @Test
    //使用POI可以在内存中创建一个Excel文件并将数据写入到这个文件,最后通过输出流将内存中的Excel文件下载到磁盘
    public void fun03() throws Exception {
        //1 创建工作簿对象
        XSSFWorkbook workbook = new XSSFWorkbook();
        //2 创建工作表对象
        XSSFSheet xssfSheet = workbook.createSheet("学生名单");
        //3 创建行
        XSSFRow row01 = xssfSheet.createRow(0);
        //4 创建列,设置内容
        row01.createCell(0).setCellValue("姓名");
        row01.createCell(1).setCellValue("性别");
        row01.createCell(2).setCellValue("地址");
        XSSFRow row02 = xssfSheet.createRow(1);
        row02.createCell(0).setCellValue("张三2");
        row02.createCell(1).setCellValue("1");// 数字字符串插入的是文本格式
        row02.createCell(2).setCellValue("深圳2");
        XSSFRow row03 = xssfSheet.createRow(2);
        row03.createCell(0).setCellValue("李四2");
        row03.createCell(1).setCellValue("1");
        row03.createCell(2).setCellValue("北京2");
        //5.通过输出流对象写到磁盘
        FileOutputStream os = new FileOutputStream("G:/student.xlsx");
        workbook.write(os);
        os.flush();
        os.close();
        workbook.close();
    }

POI工具类
package com.itheima.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
public class POIUtils {
    private final static String xls = "xls";
    private final static String xlsx = "xlsx";
    private final static String DATE_FORMAT = "yyyy/MM/dd";
    /**
     * 读入excel文件,解析后返回
     * @param file
     * @throws IOException
     */
    public static List
 /**
     * 导出Excel报表
     * @return
     */
    @RequestMapping("/exportBusinessReport.shtml")
    public Result exportBusinessReport(HttpServletRequest req, HttpServletResponse resp){
        try {
            //远程调用报表服务获取报表数据
            List
  script>
        var app = new Vue({
            el: ‘#app‘,
            data:{
                reportDate:‘‘,
                list:[]
            },
            created() {// 展示其execl文档的做法,就是将list进行循环遍历, 其后台需要做的就是查询到,将其传入list中
                axios.get("/order/getBusinessReportData.shtml").then((res)=>{
                    app.list = res.data;
                    app.reportDate=res.data.reportDate;
                    console.log("_________________________"+app.list);
                });
            },
            methods:{
                exportExcel(){ // 导出文件,前端的做法就是输出流文件
                    window.location.href = ‘/report/exportBusinessReport.shtml‘;
                }
            }
        })
    script>
                div class="box" style="height: 900px">
                    div class="excelTitle" >
                        el-button @click="exportExcel">导出Excelel-button>运营数据统计
                    div>
                    div>商家交易统计div>
                        
                    table class="exceTable" cellspacing="0" cellpadding="0" border="1px">
                        
                        tr>
                            td width="15%" align="center" class="tabletrBg">商品idtd>
                            td width="20%" align="center"  class="tabletrBg">商品名称td>
                            td width="5%" align="center"  class="tabletrBg">交易数量td>
                            td width="10%" align="center" class="tabletrBg">支付金额td>
                            td width="10%" align="center"  class="tabletrBg">支付状态td>
                            td width="15%" align="center"  class="tabletrBg">用户名称td>
                            td width="10%" align="center"  class="tabletrBg">商家名称td>
                            td width="15%" align="center"  class="tabletrBg">创建时间td>
                        tr>
             
                        tr v-for="entity in list">
                            td align="center" >{{entity.orderId}}td>
                            td align="center" >{{entity.title}}td>
                            td align="center" >{{entity.num}}td>
                            td align="center" >{{entity.totalFee}}td>
                            td align="center" >{{entity.status}}td>
                            td align="center" >{{entity.userId}}td>
                            td align="center" >{{entity.sellerId}}td>
                            td align="center" >{{entity.createTime}}td>
                        tr>
                    table>
                div>
文章标题:Apache POI和excel操作
文章链接:http://soscw.com/index.php/essay/84800.html