Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印

1. 读取

//读取excel指定sheet中的各行数据,存入二维数组,包括首行
    public static String[][] getSheetData(XSSFSheet sheet) throws IOException {
        String[][] testArray = new String[sheet.getPhysicalNumberOfRows()][];
        for(int rowId =0;rowId<sheet.getPhysicalNumberOfRows();rowId++){
                XSSFRow row = sheet.getRow(rowId);
                List<String> testSetList = new ArrayList<String>();
                for(int column=0;column<row.getPhysicalNumberOfCells();column++){
                    row.getCell(column).setCellType(Cell.CELL_TYPE_STRING);
                    testSetList.add(row.getCell(column).getStringCellValue());
                }
                testArray[rowId] = (String[])testSetList.
                        toArray(new String[testSetList.size()]);
        }
        return testArray;
    }

2. 打印

//打印二维数组
    public static void printDoubleArray(String[][] testArray) throws IOException{
        for(int i =0; i<testArray.length;i++ )
        {
            for (int j=0; j<testArray[i].length;j++)
            {
                print(testArray[i][j]+" ||");
            }
            println();
        }

    }

3. 调用

public static void main(String[] args) throws IOException {
        // TODO 自动生成的方法存根

        File file = new File("testData\\testData.xlsx");
        FileInputStream fis = new FileInputStream(file);
        @SuppressWarnings("resource")
        XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(fis));
        printDoubleArray(getSheetData(wb.getSheetAt(0)));

    }

4. 效果

时间: 2024-07-30 13:50:11

Java读取excel指定sheet中的各行数据,存入二维数组,包括首行,并打印的相关文章

44.从键盘输入12个数存入二维数组a[3][4]中,编写程序求出最大元素的值及它所在的行号和列号

//1.建立二维数组 //2.运用循环,将内容输入到数组中 //3.求出最大元素,并输出行号和列号 #include<iostream> using namespace std; int main() { int a[3][4]; int Max=0;//赋值之前需要先置为0 cout<<"please input 12 numbers: "<<endl; for(int i=0;i<3;i++)//嵌套循环,用于向二维数组中输入内容 { fo

转:用STL中的vector动态开辟二维数组

用STL中的vector动态开辟二维数组 源代码:#include <iostream>#include <vector>using namespace std;int main(){ int m, //行数     n; //列数 cout << "input value for m,n:"; cin>>m>>n;  //注意下面这一行:vector<int后两个">"之间要有空格!否则会被认

从txt文件中读取数据放在二维数组中

1.我D盘中的test.txt文件内的内容是这样的,也是随机产生的二维数组 /test.txt/ 5.440000 3.4500006.610000 6.0400008.900000 3.0300000.140000 2.7400008.920000 7.2900002.580000 7.4300001.850000 6.1300001.350000 4.280000 ... ... 2.在我的test.cpp中添加头文件,即可使用FILE类来读取txt文件中的数据 #include <stdi

PHP导出excel文件,第二步先实现自写二维数组加入模板excel文件后导出

今天主要研究数据加入EXCEL并导出的问题,先不从数据库提取数据导出,自己先写一个二维数组,然后遍历二维数组写入excel模板中导出,首先根据模板excel的内容书写对应的二维数组 $arr=array(array("111-3004394-8497032","UMN207-05MM","UMN207-05MM","2","Eric S Herbert / Entergy","600 Rockyh

smarty中函数的使用以及二维数组的使用

1.虽然讲究前后台分离,但是如果如果有的项目,前后台分离的不彻底,或者有些必须要在HTML中处理,还是要用到PHP中的函数的: <% if $Role|in_array:$menuRole[$cItem.2] %> <li><a href="<% $cItem.2 %>"><% $cItem.1 %></a></li> <% /if %> 2.碰到这种情况:多位数组,因为是循环遍历出来的,其

在二维数组中查找一个数,二维数组是从左到右,从上到下依次递增

public class FindNum { public static boolean findANum(int[][] array, int target) { int row = array.length;//行数 int cloumn = array[0].length;//列数 int i = 0; int j = cloumn - 1; boolean found = false; while(i <= row-1 && j >= 0) { if(target &l

Js中,刚学完二维数组和函数,输出杨辉三角

var a= Array(5);for ( var i= 0;i<a.length;i++){ a[i]=Array(i+1); a[i][0]=1; for(var j=0;j<a[i].length;j++) { if(i==j) { a[i][j]=1 } else{ if((i-1)>=0&&(j-1)>=0) { a[i][j]=a[i-1][j-1]+a[i-1][j];} } } }; for(var i in a){ for(var j in a[i

将字符串中的单词分割,存入二维数组后输出

思路 每次内部循环需要找到一个单词,将其存入数组.外循环遍历至字符串末尾结束. 代码 /************************************************************************* > File Name: word_split.c > Author: KrisChou > Mail:zhoujx0[email protected] > Created Time: Sun 24 Aug 2014 10:42:48 AM CST

杨氏矩阵(二维数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字)

题目要求时间复杂度小于O(N) #include<stdio.h> int find(int arr[][3], int *px, int *py,int key) { int x = 0; int y = *py - 1; while ((x < *px) && (y >= 0)) { if (arr[x][y] == key) { //下标由指针带回 *px = x; *py = y; return 0; } else if (arr[x][y] < ke