elephpant 2021-06-25 09:16 采纳率: 76.2%
浏览 24
已结题

读取excel中的数字到java程序中

Java实现,读取excel中的数字到java程序中

  • 写回答

1条回答 默认 最新

  • 404警告 2021-06-25 09:30
    关注
    public void batch<span class="hljs-constructor">Import(String <span class="hljs-params">fileName</span>, MultipartFile <span class="hljs-params">file</span>)</span> {
            <span class="hljs-keyword">if</span> (!fileName.matches(<span class="hljs-string">"^.+\\.(?i)(xls)$"</span>)<span class="hljs-operator"> && </span>!fileName.matches(<span class="hljs-string">"^.+\\.(?i)(xlsx)$"</span>)) {
                throw <span class="hljs-keyword">new</span> <span class="hljs-constructor">ValidateException(<span class="hljs-string">"上传文件类型不正确"</span>)</span>;
            }
            Workbook wb = null; <span class="hljs-comment">//创建工作簿</span>
            InputStream is = null;  <span class="hljs-comment">// io流</span>
            <span class="hljs-keyword">try</span> {
                is = file.get<span class="hljs-constructor">InputStream()</span>;
                wb = null;
                <span class="hljs-keyword">if</span> (!fileName.matches(<span class="hljs-string">"^.+\\.(?i)(xlsx)$"</span>)) {
                    wb = <span class="hljs-keyword">new</span> <span class="hljs-constructor">HSSFWorkbook(<span class="hljs-params">is</span>)</span>; <span class="hljs-comment">// 2003版</span>
                } <span class="hljs-keyword">else</span> {
                    wb = <span class="hljs-keyword">new</span> <span class="hljs-constructor">XSSFWorkbook(<span class="hljs-params">is</span>)</span>; <span class="hljs-comment">// 2007版</span>
                }
            } catch (IOException e) {
                e.print<span class="hljs-constructor">StackTrace()</span>;
            } finally {
                <span class="hljs-keyword">try</span> {
                    is.close<span class="hljs-literal">()</span>;
                } catch (IOException e) {
                    e.print<span class="hljs-constructor">StackTrace()</span>;
                }
            }
            Sheet sheet = wb.get<span class="hljs-constructor">SheetAt(0)</span>;
            <span class="hljs-keyword">for</span> (<span class="hljs-built_in">int</span> r = <span class="hljs-number">1</span>; r <= sheet.get<span class="hljs-constructor">LastRowNum()</span>; r++) {
                Row row = sheet.get<span class="hljs-constructor">Row(<span class="hljs-params">r</span>)</span>;
                <span class="hljs-keyword">if</span> (row<span class="hljs-operator"> == </span>null) {
                    continue;
                }
                <span class="hljs-keyword">if</span> (row.get<span class="hljs-constructor">Cell(0)</span><span class="hljs-operator"> == </span>null) {
                    row.create<span class="hljs-constructor">Cell(0)</span>;
                }
                <span class="hljs-keyword">if</span> (row.get<span class="hljs-constructor">Cell(2)</span> != null) {
                    row.get<span class="hljs-constructor">Cell(2)</span>.set<span class="hljs-constructor">CellType(Cell.CELL_TYPE_STRING)</span>;
                }
                String t = row.get<span class="hljs-constructor">Cell(2)</span>.<span class="hljs-keyword">to</span><span class="hljs-constructor">String()</span>;
            }
        }
    

    setCellType可以设置接收什么类型 建议都设置为string、然后在做转换比较好

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月23日
  • 已采纳回答 12月15日

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大