当前位置 : 主页 > 编程语言 > java >

基于jxl的Java导出Excel文件的简单实现

来源:互联网 收集:自由互联 发布时间:2021-06-28
Java导出excel文件 //简单Excel文件导出public static void exportExcel(String path,String fileName) throws RowsExceededException, WriteException{File file = null;OutputStream out = null;try {file = new File(path+fileName);out = new F
Java导出excel文件
//简单Excel文件导出
	public static void exportExcel(String path,String fileName) throws RowsExceededException, WriteException{
		File file = null;
		OutputStream out = null;
		try {
			file = new File(path+fileName);
			out = new FileOutputStream(file);
		} catch (FileNotFoundException e1) {
			System.out.println("文件没有找到。。。。");
			e1.printStackTrace();
		}
		//创建工作簿
		WritableWorkbook workbook = null;
		//创建新的一页sheet
		WritableSheet sheet = null;
		try {
				workbook = Workbook.createWorkbook(out);
				sheet = workbook.createSheet("sheet1",0);
				//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
				//创建字段目录行
				Label name = new Label(0,0,"姓名");
				sheet.addCell(name);
				Label id = new Label(1,0,"学号");
				sheet.addCell(id);
				Label major = new Label(2,0,"专业");
				sheet.addCell(major);
				Label class = new Label(3,0,"班级");
				sheet.addCell(class);
				
				//循环导入数据行
				for(int i=1;i<30;i++)
					Label name = new Label(0,i,"张三"+i);
					sheet.addCell(name);
					Label id = new Label(1,i,"1200"+1);
					sheet.addCell(id);
					Label major = new Label(2,i,"软件工程");
					sheet.addCell(major);
					Label class = new Label(3,i,"软工一班");
					sheet.addCell(class);
				}

			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					//把创建的内容写入到输出流中
					workbook.write();
					//关闭输出流
					workbook.close();
					out.close();
				} catch (WriteException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	}
网友评论