可以使用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();
}
}
}
}
}