Java對Excel表格的操作

來源:互聯網
上載者:User

標籤:


import java.io.File;//引入類
import java.io.IOException;
import java.util.Scanner;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.UnderlineStyle;

import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;

import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class TextOperatorExcel {// 操作Excel檔案的類


/**
* 產生一個Excel檔案
*/
public static void writeExcel(String fileName) {
WritableWorkbook wwb = null;
try {
//建立一個可寫入的工作薄(Workbook)對象
wwb = Workbook.createWorkbook(new File(fileName));
} catch (IOException e) {// 捕獲流異常
e.printStackTrace();
}
if (wwb != null) {
// 建立一個可寫入的工作表
// Workbook的createSheet方法有兩個參數,第一個是工作表的名稱,第二個是工作表在工作薄中的位置
WritableSheet ws = wwb.createSheet("sheet1", 0);
for (int i = 0; i < 10; i++) {// 迴圈添加儲存格
for (int j = 0; j < 5; j++) {
Label labelC = new Label(j, i, "這是第" + (i + 1) + "行,第"
+ (j + 1) + "列");
try {
ws.addCell(labelC);// 將產生的儲存格添加到工作表中
} catch (Exception e) {// 捕獲異常
e.printStackTrace();
}

}
}
try {
wwb.write();// 從記憶體中寫入檔案中
wwb.close();// 從記憶體中寫入檔案中
} catch (Exception e) {// 捕獲異常
e.printStackTrace();
}
}
System.out.println("產生一個Excel檔案:"+fileName+"成功!");
}

/**
* 將內容寫入
* @param fileName
* @throws Exception
*/
public static void writeContentToExcel(String fileName) throws Exception{
File tempFile=new File(fileName);
WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
//一些臨時變數,用於寫到excel中
Label l=null;
jxl.write.Number n=null;
jxl.write.DateTime d=null;

//預定義的一些字型和格式,同一個Excel中最好不要有太多格式 字形 大小 加粗 傾斜 底線 顏色
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat headerFormat = new WritableCellFormat (headerFont);

WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat titleFormat = new WritableCellFormat (titleFont);

WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat detFormat = new WritableCellFormat (detFont);

NumberFormat nf=new NumberFormat("0.00000"); //用於Number的格式
WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf);

DateFormat df=new DateFormat("yyyy-MM-dd");//用於日期的
WritableCellFormat dateFormat = new WritableCellFormat (detFont, df);

l=new Label(0, 0, "用於測試的Excel檔案", headerFormat);//建立一些儲存格,再加到sheet中
sheet.addCell(l);
//添加標題
int column=0;
l=new Label(column++, 2, "標題", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "日期", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "貨幣", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, "價格", titleFormat);
sheet.addCell(l);
//新增內容
int i=0;
column=0;
l=new Label(column++, i+3, "標題 "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "CNY", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);
sheet.addCell(n);
i++;
column=0;
l=new Label(column++, i+3, "標題 "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "SGD", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 98832, priceFormat);
sheet.addCell(n);
//設定列的寬度
column=0;
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 10);
sheet.setColumnView(column++, 20);
workbook.write();
workbook.close();
System.out.println("內容寫入"+fileName+"成功");
}

public static void readExcelInfo(String fileName)
throws Exception {// 獲得Excel檔案多少行多少列
Workbook book = Workbook.getWorkbook(new File(fileName));// 構造Workbook(工作薄)對象
Sheet sheet = book.getSheet(0);
// 得到第一列第一行的儲存格// 獲得第一個工作表對象
int columnum = sheet.getColumns(); // 得到列數
int rownum = sheet.getRows(); // 得到行數
System.out.println(columnum);
System.out.println(rownum);
for (int i = 0; i < rownum; i++) // 迴圈進行讀寫
{
for (int j = 0; j < columnum; j++) {
Cell cell1 = sheet.getCell(j, i);
String result = cell1.getContents();
System.out.print(result);
System.out.print(" \t ");
}
System.out.println();
}
book.close();//關閉(工作薄)對象
}

public static void main(String[] args) {// java程式主入口處
try {
System.out.println("1.建立Excel檔案,輸入Excel檔案名稱(包括路徑和尾碼)");
Scanner scan = new Scanner(System.in);
final String fileName = scan.next();//獲得鍵盤值
writeExcel(fileName);//調用產生Excel方法

System.out.println("2.將內容寫入Excel檔案");
writeContentToExcel(fileName);//調用將內容寫入Excel方法

System.out.println("3.讀取Excel檔案");
readExcelInfo(fileName);
} catch (Exception e) {//捕獲異常
e.printStackTrace();
}
}
}

Java對Excel表格的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.