java邮件中添加excel_java 向Excel中添加数据

先看下结果图:

0818b9ca8b590ca3270a3433284dd417.png

代码如下:

package com.excel;

import java.io.File;

import java.io.IOException;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

import jxl.write.biff.RowsExceededException;

public class ExcelWrite{

public static void writeExcel(String fileName){

WritableWorkbook wwb = null;

double Array[][] = new double[10][10];

for(int i = 0; i < 10; i ++){

for(int j = 0; j < 10; j ++){

Array[i][j] = Math.random()%10;

}

}

try {

//首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象

wwb = Workbook.createWorkbook(new File(fileName));

} catch (IOException e) {

e.printStackTrace();

}

if(wwb!=null){

//创建一个可写入的工作表

//Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置

WritableSheet ws = wwb.createSheet("工作表名称", 0);

Label label0 = new Label(0,0,"词项|主题");

try {

//将生成的单元格添加到工作表中

ws.addCell(label0);

} catch (RowsExceededException e) {

e.printStackTrace();

} catch (WriteException e) {

e.printStackTrace();

}

for(int i=1;i<=10;i++){

Label labelC = new Label(i, 0, "T"+i);

try {

//将生成的单元格添加到工作表中

ws.addCell(labelC);

} catch (RowsExceededException e) {

e.printStackTrace();

} catch (WriteException e) {

e.printStackTrace();

}

}

for(int j = 1; j <= 10; j ++){

Label labelC = new Label(0, j, "W"+j);

try {

//将生成的单元格添加到工作表中

ws.addCell(labelC);

} catch (RowsExceededException e) {

e.printStackTrace();

} catch (WriteException e) {

e.printStackTrace();

}

}

for(int j = 1; j <= 10; j ++){

for(int i = 1; i <= 10; i ++){

String str = Double.toString(Array[j-1][i-1]);

Label label = new Label(j, i , str);

try {

ws.addCell(label);

} catch (RowsExceededException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (WriteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

try {

//从内存中写入文件中

wwb.write();

//关闭资源,释放内存

wwb.close();

} catch (IOException e) {

e.printStackTrace();

} catch (WriteException e) {

e.printStackTrace();

}

}

//循环对每一个单元格进行赋值

//定位行

}

public static void main(String[] args) {

String fileName = "E:\\IR1\\LDA迭代结果\\EXCEL结果保存\\test\\testt.xls";

ExcelWrite.writeExcel(fileName);

}

}

参考的文章和博客如下:

http://blog.csdn.net/mr_walter/article/details/8445905

http://hi.baidu.com/fbdfp/item/211402be43671d43bb0e123e

关于这个例子的程序包下载网址如下:

http://download.csdn.net/detail/a15994269853/7340965

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值