zoukankan      html  css  js  c++  java
  • 导出Excel数据

    先要导入jxl架包,其中的abc.xls为测试Excel,具体代码如下,仅供参考:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    /**
     * 读取excel公共方法
     */
    public class ExcelTest {
    	/**
    	 * 
    	 * @param excelFile 读取文件对象
    	 * @param rowNum 从第几行开始读,如果有一行表头则从第二行开始读
    	 * @return
    	 * @throws BiffException
    	 * @throws IOException
    	 */
    	public static List<String[]> readExcel(File excelFile,int rowNum) throws BiffException,
    			IOException {
    		// 创建一个list 用来存储读取的内容
    		List<String[]> list = new ArrayList<String[]>();
    		Workbook rwb = null;
    		Cell cell = null;
    		// 创建输入流
    		InputStream stream = new FileInputStream(excelFile);
    		// 获取Excel文件对象
    		rwb = Workbook.getWorkbook(stream);
    		// 获取文件的指定工作表 默认的第一个
    		Sheet sheet = rwb.getSheet(0);
    		// 行数(表头的目录不需要,从1开始)
    		for (int i = rowNum-1; i < sheet.getRows(); i++) {
    			// 创建一个数组 用来存储每一列的值
    			String[] str = new String[sheet.getColumns()];
    			// 列数
    			for (int j = 0; j < sheet.getColumns(); j++) {
    				// 获取第i行,第j列的值
    				cell = sheet.getCell(j, i);
    				str[j] = cell.getContents();
    			}
    			// 把刚获取的列存入list
    			list.add(str);
    		}
    		// 返回值集合
    		return list;
    	}
    	
    	public static void main(String[] args) {
    		String excelFileName = "abc.xls";
    		try {
    			List<String[]> list = ExcelTest.readExcel(new File(excelFileName),1);
    			for (int i = 0; i < list.size(); i++) {
    				String[] str = (String[])list.get(i);
    				for (int j = 0; j < str.length; j++) {
    					System.out.print(str[j]+" ");
    				}
    				System.out.println();
    			}
    		} catch (BiffException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

     

    测试excel文件abc.xls截图如下:

     

    运行结果如下所示:

  • 相关阅读:
    2019/2/3从字符串中删除指定的字符
    2019/2/3求组合数
    2019/2/3统计各成绩段的学生人数
    2019/2/3摄氏一华氏温度转换表
    2019/1/29有选择的复制字符串
    2019/1/28数字的移动
    2019/1/2810个整数的数据处理
    2019/1/27从三个数中找出最大的数(函数和宏)
    2019/1/23编写函数统计字符串中字母、数字、空格和其它字符的个数
    Jenkins 执行python脚本
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3837456.html
Copyright © 2011-2022 走看看