如何把List里面的值循环相加

如何把List里面的值循环相加,第1张

1、先新建一个JAVA类,包含一个main方法。

2、先准备一个list集合,是map类型的。

3、然后准备两个map集合,更多也可以。

4、把上面准备的map集合添加到list里面去。

5、用一个for循环把每一个map循环出来然后修改就可以了。

6、最后看一下效果。

首先,你要有poi库,这是 *** 作office文档的

然后,给你贴一个excel *** 作类,你读一下,应该就可以自己写调用了

import java.io.FileInputStream

import java.io.IOException

import java.io.InputStream

import java.util.ArrayList

import java.util.List

import org.apache.poi.hssf.usermodel.HSSFCell

import org.apache.poi.hssf.usermodel.HSSFRow

import org.apache.poi.hssf.usermodel.HSSFSheet

import org.apache.poi.hssf.usermodel.HSSFWorkbook

import org.apache.poi.xssf.usermodel.XSSFCell

import org.apache.poi.xssf.usermodel.XSSFRow

import org.apache.poi.xssf.usermodel.XSSFSheet

import org.apache.poi.xssf.usermodel.XSSFWorkbook

import com.b510.common.Common

import com.b510.excel.util.Util

import com.b510.excel.vo.Student

/**

 * @author Hongten

 * @created 2014-5-20

 */

public class ReadExcel {

    

    /**

     * read the Excel file

     * @param path the path of the Excel file

     * @return

     * @throws IOException

     */

    public List<Student> readExcel(String path) throws IOException {

        if (path == null || Common.EMPTY.equals(path)) {

            return null

        } else {

            String postfix = Util.getPostfix(path)

            if (!Common.EMPTY.equals(postfix)) {

                if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {

                    return readXls(path)

                } else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {

                    return readXlsx(path)

                }

            } else {

                System.out.println(path + Common.NOT_EXCEL_FILE)

            }

        }

        return null

    }

    /**

     * Read the Excel 2010

     * @param path the path of the excel file

     * @return

     * @throws IOException

     */

    public List<Student> readXlsx(String path) throws IOException {

        System.out.println(Common.PROCESSING + path)

        InputStream is = new FileInputStream(path)

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is)

        Student student = null

        List<Student> list = new ArrayList<Student>()

        // Read the Sheet

        for (int numSheet = 0 numSheet < xssfWorkbook.getNumberOfSheets() numSheet++) {

            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet)

            if (xssfSheet == null) {

                continue

            }

            // Read the Row

            for (int rowNum = 1 rowNum <= xssfSheet.getLastRowNum() rowNum++) {

                XSSFRow xssfRow = xssfSheet.getRow(rowNum)

                if (xssfRow != null) {

                    student = new Student()

                    XSSFCell no = xssfRow.getCell(0)

                    XSSFCell name = xssfRow.getCell(1)

                    XSSFCell age = xssfRow.getCell(2)

                    XSSFCell score = xssfRow.getCell(3)

                    student.setNo(getValue(no))

                    student.setName(getValue(name))

                    student.setAge(getValue(age))

                    student.setScore(Float.valueOf(getValue(score)))

                    list.add(student)

                }

            }

        }

        return list

    }

    /**

     * Read the Excel 2003-2007

     * @param path the path of the Excel

     * @return

     * @throws IOException

     */

    public List<Student> readXls(String path) throws IOException {

        System.out.println(Common.PROCESSING + path)

        InputStream is = new FileInputStream(path)

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is)

        Student student = null

        List<Student> list = new ArrayList<Student>()

        // Read the Sheet

        for (int numSheet = 0 numSheet < hssfWorkbook.getNumberOfSheets() numSheet++) {

            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet)

            if (hssfSheet == null) {

                continue

            }

            // Read the Row

            for (int rowNum = 1 rowNum <= hssfSheet.getLastRowNum() rowNum++) {

                HSSFRow hssfRow = hssfSheet.getRow(rowNum)

                if (hssfRow != null) {

                    student = new Student()

                    HSSFCell no = hssfRow.getCell(0)

                    HSSFCell name = hssfRow.getCell(1)

                    HSSFCell age = hssfRow.getCell(2)

                    HSSFCell score = hssfRow.getCell(3)

                    student.setNo(getValue(no))

                    student.setName(getValue(name))

                    student.setAge(getValue(age))

                    student.setScore(Float.valueOf(getValue(score)))

                    list.add(student)

                }

            }

        }

        return list

    }

    @SuppressWarnings("static-access")

    private String getValue(XSSFCell xssfRow) {

        if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {

            return String.valueOf(xssfRow.getBooleanCellValue())

        } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {

            return String.valueOf(xssfRow.getNumericCellValue())

        } else {

            return String.valueOf(xssfRow.getStringCellValue())

        }

    }

    @SuppressWarnings("static-access")

    private String getValue(HSSFCell hssfCell) {

        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {

            return String.valueOf(hssfCell.getBooleanCellValue())

        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {

            return String.valueOf(hssfCell.getNumericCellValue())

        } else {

            return String.valueOf(hssfCell.getStringCellValue())

        }

    }

}

百度关键字是:java的poi技术读取Excel[2003-2007,2010],这个帖子下面甚至提供了代码下载。

package com.xxt.lyh

import java.util.ArrayList

import java.util.List

public class Test {

public static void main(String[] args) {

List<String>list = new ArrayList<String>()

list.add("张三")

list.add("李四")

list.add("王五")

list.add("赵六")

// for循环。

for (int i = 0i <list.size()i++) {

String name = list.get(i)

System.out.println(name)

}

// 增强型for循环,java5开始引入。

System.out.println()

for (String name : list) {

System.out.println(name)

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11606204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存