`
huanglz19871030
  • 浏览: 241372 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jxl操作excel(将数据库中的数据导出到execl文件)

阅读更多

  1.首先工程中加入jxl.jar文件。

 

    2.开始写java代码。如下:

Java代码 复制代码
  1. package toreports;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileNotFoundException;   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7. import java.util.Date;   
  8. import java.util.List;   
  9.   
  10. import jxl.Workbook;   
  11. import jxl.write.Label;   
  12. import jxl.write.Number;   
  13. import jxl.write.WritableCell;   
  14. import jxl.write.WritableSheet;   
  15. import jxl.write.WritableWorkbook;   
  16. import jxl.write.WriteException;   
  17. import jxl.write.biff.RowsExceededException;   
  18.   
  19.   
  20. import common.Utils;   
  21.   
  22. public class ToExcel {  
package toreports;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


import common.Utils;

public class ToExcel {

 

Java代码 复制代码
  1.              /**  
  2.  * jxl将数据导入excel  
  3.  *   
  4.  */  
  5. public void jxl2Excel() {   
  6.     FileOutputStream fout = null;   
  7.     try {   
  8.         // 1.生成excel文件   
  9.         fout = new FileOutputStream(new File("file/data.xls"));   
  10.   
  11.         // 2.生成工作簿   
  12.         WritableWorkbook wb = Workbook.createWorkbook(fout);   
  13.         // 3.生成工作表   
  14.         WritableSheet sheet = wb.createSheet("工作表1"0);   
  15.         // 4.生成单元格   
  16.         Label label = new Label(00"编号");   
  17.         // 5.工作表中加入单元格   
  18.         sheet.addCell(label);   
  19.         label = new Label(10"姓名");   
  20.         sheet.addCell(label);   
  21.         label = new Label(20"出生日期");   
  22.         sheet.addCell(label);   
  23.         List totalList = Utils.getAllDatas();   
  24.   
  25.         for (int i = 0; i < totalList.size(); i++) {   
  26.             List list = (List) totalList.get(i);   
  27.             for (int j = 0; j < list.size(); j++) {   
  28.                 label = new Label(j, i + 1, list.get(j).toString());   
  29.                 sheet.addCell(label);   
  30.             }   
  31.         }   
  32.   
  33.         wb.write();   
  34.         wb.close();   
  35.   
  36.     } catch (IOException e) {   
  37.         e.printStackTrace();   
  38.     } catch (RowsExceededException e) {   
  39.         // TODO Auto-generated catch block   
  40.         e.printStackTrace();   
  41.     } catch (WriteException e) {   
  42.         // TODO Auto-generated catch block   
  43.         e.printStackTrace();   
  44.     } finally {   
  45.         if (fout != null) {   
  46.             try {   
  47.                 fout.close();   
  48.             } catch (IOException e) {   
  49.                 e.printStackTrace();   
  50.             }   
  51.         }   
  52.     }   
  53.   
  54. }   
              /**
	 * jxl将数据导入excel
	 * 
	 */
	public void jxl2Excel() {
		FileOutputStream fout = null;
		try {
			// 1.生成excel文件
			fout = new FileOutputStream(new File("file/data.xls"));

			// 2.生成工作簿
			WritableWorkbook wb = Workbook.createWorkbook(fout);
			// 3.生成工作表
			WritableSheet sheet = wb.createSheet("工作表1", 0);
			// 4.生成单元格
			Label label = new Label(0, 0, "编号");
			// 5.工作表中加入单元格
			sheet.addCell(label);
			label = new Label(1, 0, "姓名");
			sheet.addCell(label);
			label = new Label(2, 0, "出生日期");
			sheet.addCell(label);
			List totalList = Utils.getAllDatas();

			for (int i = 0; i < totalList.size(); i++) {
				List list = (List) totalList.get(i);
				for (int j = 0; j < list.size(); j++) {
					label = new Label(j, i + 1, list.get(j).toString());
					sheet.addCell(label);
				}
			}

			wb.write();
			wb.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fout != null) {
				try {
					fout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

 

      总结:对比着poi和jxl方式,其实使用的时候的思路是差不多的。个人感觉,其实仅仅是用的类不同而已,操作的思路是完全相同的。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics