简单的java写入到excel中的代码

来源:互联网 发布:如何破解红蜘蛛软件 编辑:程序博客网 时间:2024/05/05 14:31

package com.wondersgroup;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
* 向excel中插入数据
* @author user
*
*/
public class WriteExcel {
static String path=”G:\***\**\**(20160219).xls”;//需要写入文件的路径
public void setContent(String path) {
try {
//创建一个excel表对象
HSSFWorkbook hk=new HSSFWorkbook();
//创建excel的工作表
HSSFSheet hs=hk.createSheet(“111”);
//将信息写入到工作表中行和列中
HSSFRow hr =hs.createRow(0);
HSSFCell hCell=hr.createCell(0);
//将要写的内容写入到单元格中
hCell.setCellValue(“asdfajsd”);
//设置输出流将数据写入到excel表中
FileOutputStream fos=new FileOutputStream(path);
hk.write(fos);
fos.close();
System.out.println(“++++++++”);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new WriteExcel().setContent(path);
}

}

这是需要导入的jar包

0 0
原创粉丝点击