本文目录一览:
java怎么往excel写数据
public void export(ListPSbLnode li, String dateString,String[] title) throws WriteException, IOException {
// 准备设置excel工作表的标题
// 创建Excel工作薄
WritableWorkbook wwb = null;
try {
// 输出的excel的路径
String filePath1 = Const.pathStr+Const.pathStr4+Const.pathStr3;
File file = new File(filePath1);
if(!file.exists()){
file.mkdir();
}
String filePath=filePath1+Const.pathStr4+Const.pathStr6+dateString+Const.pathStr5;
// 新建立一个jxl文件,即在C盘下生成testJXL.xls
OutputStream os = new FileOutputStream(filePath);
wwb = Workbook.createWorkbook(os);
// 添加第一个工作表并设置第一个Sheet的名字
WritableSheet sheet = wwb.createSheet("设备清单", 0);
Label label;
for (int i = 0; i title.length; i++) {
// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
// 在Label对象的子对象中指明单元格的位置和内容
label = new Label(i, 0, title[i]);
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
}
for (int i = 0; i li.size(); i++) {
int j = 0;
j = i + 1;
//填充单元格
//获取区域名称
label = new Label(0, j, li.get(i).getQyName());
sheet.addCell(label);
//获取区域名称
label = new Label(1, j, li.get(i).getJzName());
sheet.addCell(label);
//获取设备名称
label = new Label(2, j, li.get(i).getLnodeName());
sheet.addCell(label);
// //获取设备类型名称
label = new Label(3, j, li.get(i).getSbxh());
sheet.addCell(label);
//获取运行状态
label = new Label(4, j, li.get(i).getYxzh());
sheet.addCell(label);
//获取删除状态
label = new Label(5, j, li.get(i).getDeleteFlag());
sheet.addCell(label);
//获取启用状态
label = new Label(6, j, li.get(i).getQyzt());
sheet.addCell(label);
//获取设备投运日期
label = new Label(7, j, li.get(i).getSbtyri());
sheet.addCell(label);
//获取使用年限
jxl.write.Number numb1 = new jxl.write.Number(8, j, li.get(i).getSynx());
sheet.addCell(numb1);
//获取区域名称
label = new Label(9, j, li.get(i).getAddUser());
sheet.addCell(label);
//获取区域名称
label = new Label(10, j, li.get(i).getUpdUser());
sheet.addCell(label);
//获取区域名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String newdate = sdf.format(li.get(i).getUpdTime());
label = new Label(11, j, newdate);
sheet.addCell(label);
//获取区域名称
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
String newdate2 = sdf2.format(li.get(i).getAddTime());
label = new Label(12, j, newdate2);
sheet.addCell(label);
//获取区域名称
label = new Label(13, j, li.get(i).getZcbh());
sheet.addCell(label);
//获取区域名称
label = new Label(14, j, li.get(i).getSbcs());
sheet.addCell(label);
//获取区域名称
jxl.write.Number numb2 = new jxl.write.Number(15, j, li.get(i)
.getSbll());
sheet.addCell(numb2);
//获取区域名称
label = new Label(16, j, li.get(i).getRldw());
sheet.addCell(label);
//获取区域名称
label = new Label(17, j, li.get(i).getWxghjl());
sheet.addCell(label);
}
// 写入数据
wwb.write();
} catch (Exception e) {
e.printStackTrace();
}finally{
// 关闭文件
wwb.close();
}
}
Const文件:
/**路径:C盘*/
public static String pathStr = "C:";
/**路径://*/
public static String pathStr2 = "//";
/**文件夹:workspace*/
public static String pathStr3 = "exportFile";
/**文件名:world*/
public static String pathStr6 = "Equipment";
/**路径:/*/
public static String pathStr4 = "/";
/**路径:.xls*/
public static String pathStr5 = ".xls";
怎样用java向excel中写数据
public void export(ListPSbLnode li, String dateString,String[] title) throws WriteException, IOException {
// 准备设置excel工作表的标题
// 创建Excel工作薄
WritableWorkbook wwb = null;
try {
// 输出的excel的路径
String filePath1 = Const.pathStr+Const.pathStr4+Const.pathStr3;
File file = new File(filePath1);
if(!file.exists()){
file.mkdir();
}
String filePath=filePath1+Const.pathStr4+Const.pathStr6+dateString+Const.pathStr5;
// 新建立一个jxl文件,即在C盘下生成testJXL.xls
OutputStream os = new FileOutputStream(filePath);
wwb = Workbook.createWorkbook(os);
// 添加第一个工作表并设置第一个Sheet的名字
WritableSheet sheet = wwb.createSheet("设备清单", 0);
Label label;
for (int i = 0; i title.length; i++) {
// Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z
// 在Label对象的子对象中指明单元格的位置和内容
label = new Label(i, 0, title[i]);
// 将定义好的单元格添加到工作表中
sheet.addCell(label);
}
for (int i = 0; i li.size(); i++) {
int j = 0;
j = i + 1;
//填充单元格
//获取区域名称
label = new Label(0, j, li.get(i).getQyName());
sheet.addCell(label);
//获取区域名称
label = new Label(1, j, li.get(i).getJzName());
sheet.addCell(label);
//获取设备名称
label = new Label(2, j, li.get(i).getLnodeName());
sheet.addCell(label);
// //获取设备类型名称
label = new Label(3, j, li.get(i).getSbxh());
sheet.addCell(label);
//获取运行状态
label = new Label(4, j, li.get(i).getYxzh());
sheet.addCell(label);
//获取删除状态
label = new Label(5, j, li.get(i).getDeleteFlag());
sheet.addCell(label);
//获取启用状态
label = new Label(6, j, li.get(i).getQyzt());
sheet.addCell(label);
//获取设备投运日期
label = new Label(7, j, li.get(i).getSbtyri());
sheet.addCell(label);
//获取使用年限
jxl.write.Number numb1 = new jxl.write.Number(8, j, li.get(i).getSynx());
sheet.addCell(numb1);
//获取区域名称
label = new Label(9, j, li.get(i).getAddUser());
sheet.addCell(label);
//获取区域名称
label = new Label(10, j, li.get(i).getUpdUser());
sheet.addCell(label);
//获取区域名称
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String newdate = sdf.format(li.get(i).getUpdTime());
label = new Label(11, j, newdate);
sheet.addCell(label);
//获取区域名称
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
String newdate2 = sdf2.format(li.get(i).getAddTime());
label = new Label(12, j, newdate2);
sheet.addCell(label);
//获取区域名称
label = new Label(13, j, li.get(i).getZcbh());
sheet.addCell(label);
//获取区域名称
label = new Label(14, j, li.get(i).getSbcs());
sheet.addCell(label);
//获取区域名称
jxl.write.Number numb2 = new jxl.write.Number(15, j, li.get(i)
.getSbll());
sheet.addCell(numb2);
//获取区域名称
label = new Label(16, j, li.get(i).getRldw());
sheet.addCell(label);
//获取区域名称
label = new Label(17, j, li.get(i).getWxghjl());
sheet.addCell(label);
}
// 写入数据
wwb.write();
} catch (Exception e) {
e.printStackTrace();
}finally{
// 关闭文件
wwb.close();
}
}
Const文件:
/**路径:C盘*/
public static String pathStr = "C:";
/**路径://*/
public static String pathStr2 = "//";
/**文件夹:workspace*/
public static String pathStr3 = "exportFile";
/**文件名:world*/
public static String pathStr6 = "Equipment";
/**路径:/*/
public static String pathStr4 = "/";
/**路径:.xls*/
public static String pathStr5 = ".xls";
如何用java把数据写入到excel
需要导入jxl.jar
搭建环境
将下载后的文件解包,得到jxl.jar,放入classpath,安装就完成了。
创建文件
拟生成一个名为“测试数据.xls”的Excel文件,其中第一个工作表被命名为“第一页”,大致效果如下:
代码(CreateXLS.java):
//生成Excel的类
import java.io.*;
import jxl.*;
import jxl.write.*;
public class CreateXLS
{
public static void main(String args[])
{
try
{
//打开文件
WritableWorkbook book=
Workbook.createWorkbook(new File(“测试.xls”));
//生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet=book.createSheet(“第一页”,0);
//在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
//以及单元格内容为test
Label label=new Label(0,0,”test”);
//将定义好的单元格添加到工作表中
sheet.addCell(label);
/*生成一个保存数字的单元格
必须使用Number的完整包路径,否则有语法歧义
单元格位置是第二列,第一行,值为789.123*/
jxl.write.Number number = new jxl.write.Number(1,0,789.123);
sheet.addCell(number);
//写入数据并关闭文件
book.write();
book.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
编译执行后,会在当前位置产生一个Excel文件。