可以使用apache的poi包,这是一个开源项目,可方便读写excel文件,这里有一些例子:
http://dev.csdn.net/develop/article/73/73804.shtm
http://www.chinahtm.cn/program/26385.html
Ⅱ java代码创建一个Excel文件
importjava.io.File;
publicclassTest{
publicstaticvoidmain(String[]args)throwsException{
Filef=newFile("d:/1.xls");
f.createNewFile();
}
}
好吧,之前没看到“用poi”这句话。现在下面的是用poi的:
importjava.io.FileOutputStream;
importjava.io.IOException;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
publicclassTest{
publicstaticvoidmain(String[]args){
try{
HSSFWorkbookworkbook=newHSSFWorkbook();
FileOutputStreamfileOut=newFileOutputStream("D:/2.xls");
workbook.write(fileOut);
fileOut.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
Ⅲ 利用java怎么实现生成报表(Excel文件)
这是从Tabel导出数据到Excel的一个例子:jxl.jar包你可以去网上找,有很多资源,如果没找到,也可以留个邮箱,我发给你
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class TableToExcel {
public static void export(File file,String heading,String note,JTable table) {
WritableWorkbook workbook = null;// 创建工作薄
try {
if(file.exists()) {//如果文件存在
workbook = Workbook.createWorkbook(file, Workbook.getWorkbook(file));
} else {//如果文件不存在
workbook = Workbook.createWorkbook(file);
}
// 创建工作表
WritableSheet sheet = workbook.createSheet(heading, workbook.getNumberOfSheets());
//获取jtable 的行数和列数
int rowNum = table.getRowCount();
int colNum = table.getColumnCount();
fillHeader(sheet,heading,colNum);//填写主标题
fillColName(sheet,table,colNum);//填写列名
fillCell(sheet,table,colNum,rowNum);// 填写数据
fillNote(sheet,note,colNum,rowNum);//填写签名档
workbook.write();//写入数据
workbook.close();//关闭
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "导入数据前请关闭工作表");
} catch (BiffException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/**
* 填写 表标题
* @param sheet
* @param heading
* @param colNum
* @throws WriteException
*/
private static void fillHeader(WritableSheet sheet,String heading,int colNum) throws WriteException {
WritableFont font = new WritableFont(WritableFont.ARIAL, 18, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
format.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直居中对齐
sheet.mergeCells(0,0, colNum-1, 0);//合并单元格
sheet.setRowView(0, 600); // 设置行高
sheet.addCell(new Label(0,0,heading,format));//填写主标题
}
/**
* 填写列名
* @param sheet
* @param table
* @param colNum
* @throws WriteException
*/
private static void fillColName(WritableSheet sheet,JTable table,int colNum) throws WriteException {
WritableFont font = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
sheet.setColumnView(0, 15);// 设置列宽
for(int col = 0; col < colNum;col++) {
Label colName = new Label(col,1,table.getModel().getColumnName(col),format);
sheet.addCell(colName);
}
}
/**
* 填写表格数据到excel
* @param sheet
* @param table
* @param colNum
* @param rowNum
* @throws WriteException
*/
private static void fillCell(WritableSheet sheet,JTable table,int colNum,int rowNum) throws WriteException {
WritableFont font = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//设置字体
WritableCellFormat format = new WritableCellFormat(font);//新建格式化对象
format.setAlignment(Alignment.CENTRE);//设置水平居中
for(int col = 0;col < colNum;col++) {
for(int row = 1;row <= rowNum -1;row++) {//填写数据
String value = "";
if(table.getModel().getValueAt(row -1, col) != null)
value = table.getModel().getValueAt(row -1, col).toString();
sheet.addCell(new Label(col,row + 1,value));
}
}
}
/**
* 填写 签名档
* @param sheet
* @param inscribe
* @param colNum
* @param rowNum
* @throws WriteException
*/
private static void fillNote(WritableSheet sheet,String inscribe,int colNum,int rowNum) throws WriteException {
if( inscribe == null || inscribe.length() < 1 ) {
inscribe = "导出时间: "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
WritableFont font = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);// 定义字体
WritableCellFormat format = new WritableCellFormat(font);// 定义格式化对象
format.setAlignment(Alignment.RIGHT);// 水平居中显示
sheet.mergeCells(0, rowNum+3, colNum - 1, rowNum+3);// 合并单元格
sheet.addCell(new Label(0, rowNum+3, inscribe, format));// 填写工作表
}
}
Ⅳ java简单创建excel
使用jxl或者poi的一个jar包;这两个都差不多,自己看着用;jxl:
WritableWorkbook wwb = jxl.Workbook.createWorkbook(new File(TargetFileName));
WritableSheet ws = wwb.createSheet("123",0);
label = new jxl.write.Label(0, 0, “456”);
ws.addCell(label);
poi:
HSSFWorkbook workbook = new HSSFWorkbook();
在Excel工作簿中建专一工作表属
HSSFSheet sheet = workbook.createSheet("123");
希望对你有用了
Ⅳ 利用java怎么实现生成报表(Excel文件)
JAVA POI 组件//创建来HSSFWorkbook对象源
HSSFWorkbook wb = new HSSFWorkbook();
//创建HSSFSheet对象
HSSFSheet sheet = wb.createSheet("sheet0");
//创建HSSFRow对象
HSSFRow row = sheet.createRow((short)0);
//创建HSSFCell对象
HSSFCell cell=row.createCell((short)0);
//用来处理中文问题
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//设置单元格的值
cell.setCellValue("单元格中的中文");
//定义你需要的输出流
OutputStream out = new FileOutputStream("viwo.xls");
//输出Excel
Ⅵ 如何用java代码生成一个大数据的excel文件
POI包解析 或者 只是xls的话 用 jxl 也行 poi 全支持 xls 和xlsx
然后写入 保存 ok
HSSFSheet sheet= null;
(int sherrt= 0; sherrt <wr.getNumberOfSheets();sherrt++){
sheet = wr.getSheetAt(sherrt); // 获得sheet工作簿HSSFSheet
for(int i = 0 ; i<=sheet.getLastRowNum(); i++){
HSSFRow row = sheet.getRow(i);//获得行数
Iterator o = row.iterator(); //得到每行的值
int j= 0 ;
while(o.hasNext()){
if(!key){
kk = o.next().toString();
if(StrC.getSimilarityRatio(kk, Vle[j])>0.7){j++;}
}else{
GetVAR[j] = o.next().toString();
j++;
}
}
这个是得到 也可以写入
Ⅶ 如何用java对EXECL插入图片
在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片。所以要在Excel中插入图片,三步就可以搞定。一、获取HSSFPatriarch对象,二、new HSSFClientAnchor对象,三、调用createPicture方法即可。实现倒是非常容易实现,如果想把它做好还是有点儿难度的。这里我们先插入一张图片:
public class ExcelImageTest {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
try {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("F:/图片/照片/无名氏/小昭11.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("test picture");
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);
anchor.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("D:/测试Excel.xls");
// 写入excel文件
wb.write(fileOut);
System.out.println("----Excle文件已生成------");
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fileOut != null){
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}