• Jxl创建Excel文件和解析Excel文件


    import java.io.File;
    import jxl.Workbook;
    import jxl.write.Label;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    
    public class JxlExpExcel {
    	/**
    	 * Jxl创建Excel文件
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//用数组定义一个表头
    		String[] title={"id","name","sex"};
            //创建Excel文件
    		File file=new File("d:/jxl_test.xls");
    		try {
    			file.createNewFile();
    			//创建工作薄
    			WritableWorkbook workbook=Workbook.createWorkbook(file);
    			//创建sheet
    			WritableSheet sheet=workbook.createSheet("sheet1", 0);
    			Label lable=null;
    			for (int i = 0; i < title.length; i++) {
    				lable=new Label(i,0,title[i]);
    				sheet.addCell(lable);
    			}
    			//追加数据
    			for (int i = 1; i < 100; i++) {
    				lable=new Label(0,i,"a"+i);
    				sheet.addCell(lable);
    				lable=new Label(1,i,"user"+i);
    				sheet.addCell(lable);
    				lable=new Label(2,i,"男");
    				sheet.addCell(lable);
    			}
    			//写入数据
    			workbook.write();
    			//关闭流
    			workbook.close();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    

      

    import java.io.File;
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    
    public class JxlReadExcel {
    	/**
    	 * 解析Excel文件
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
            try {
            	//创建workbook
    			Workbook workbook=Workbook.getWorkbook(new File("d:/jxl_test.xls"));
    			//获取第一个工作表sheet
    			Sheet sheet=workbook.getSheet(0);
    			//获取数据
    			for (int i = 0; i < sheet.getRows(); i++) {
    				for (int j = 0; j < sheet.getColumns(); j++) {
    					Cell cell=sheet.getCell(j,i);
    					System.out.print(cell.getContents()+" ");
    				}
    				System.out.println();
    			}
    			//关闭流
    			workbook.close();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    

      

  • 相关阅读:
    python实现模拟登录
    python进阶八_警告和异常
    我的软考之路(六)——数据结构与算法(4)之八大排序
    Modern source-to-source transformation with Clang and libTooling
    heibernate增删改查总结一下自己的不足
    hibernate之增删改查demo
    前台之boostrap
    <input value="hidden">的作用
    Active Desktop--桌面字体背景被修改
    中文乱码问题解决方法总结
  • 原文地址:https://www.cnblogs.com/Jansens520/p/7728047.html
Copyright © 2020-2023  润新知