亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android開發(fā)實(shí)現(xiàn)生成excel的方法詳解

 更新時(shí)間:2017年10月07日 02:23:24   作者:showCar  
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)生成excel的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android生成Excel的具體步驟與存儲(chǔ)、導(dǎo)入、添加等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)生成excel的方法。分享給大家供大家參考,具體如下:

都說程序員不爽產(chǎn)品經(jīng)理,其實(shí)有的時(shí)候遇到一些奇葩的后臺開發(fā)人員也會(huì)很不順心。最近項(xiàng)目有這樣一個(gè)要求,要生成一個(gè)excel然后發(fā)郵件給客戶。結(jié)果后臺人員直接把這個(gè)功能扔給客戶端,理由是后臺不好實(shí)現(xiàn)。聽到這也就只能自己實(shí)現(xiàn)了(分分鐘就想來個(gè)螺旋王扣它頭上)。這篇博客講下如下在android中生成excel表并存到本地。先看下生成后的效果圖:

初始化數(shù)據(jù)

首先我們要先造下測試數(shù)據(jù),這里我把數(shù)據(jù)寫死在一個(gè)常量類Const中,如下:

public class Const {
  public interface OrderInfo{
    public static final String[][] orderOne = new String[][] {{ "123", "九龍", "13294352311",
      "武漢市關(guān)山口" },{ "124", "咱家", "13294352312",
      "武漢市水果湖" },{ "125", "陳家", "13294352315",
      "武漢市華師" },{ "126", "李", "13294352316",
      "武漢市楊家灣" }};
  }
}

理論上這些數(shù)據(jù)是從后臺讀過來的。

本文模擬打印訂單的信息,所以這里還需要一個(gè)訂單Model類:

public class Order implements Serializable {
  public String id;
  public String restPhone;
  public String restName;
  public String receiverAddr;
  public Order(String id,String restPhone, String restName, String receiverAddr) {
    this.id = id;
    this.restPhone = restPhone;
    this.restName = restName;
    this.receiverAddr = receiverAddr;
  }
}

存內(nèi)存卡

接下來我們要判斷一下內(nèi)存卡是否存在,內(nèi)存是否足夠大。先獲取指定目錄下內(nèi)存的大?。?/p>

/** 獲取SD可用容量 */
private static long getAvailableStorage(Context context) {
    String root = context.getExternalFilesDir(null).getPath();
    StatFs statFs = new StatFs(root);
    long blockSize = statFs.getBlockSize();
    long availableBlocks = statFs.getAvailableBlocks();
    long availableSize = blockSize * availableBlocks;
    // Formatter.formatFileSize(context, availableSize);
    return availableSize;
}

這里用到的路徑是getExternalFilesDir,它指定的是SDCard/Android/data/你的應(yīng)用的包名/files/ 目錄這個(gè)目錄,它用來放一些長時(shí)間保存的數(shù)據(jù),當(dāng)應(yīng)用被卸載時(shí),會(huì)同時(shí)會(huì)刪除。類似這種情況的還有g(shù)etExternalCacheDir方法,只是它一般用來存放臨時(shí)文件。之后通過StatFs來計(jì)算出可用容量的大小。

接下來在寫入excel前對內(nèi)存進(jìn)行判斷,如下:

if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)&&getAvailableStorage()>1000000) {
  Toast.makeText(context, "SD卡不可用", Toast.LENGTH_LONG).show();
  return;
}
File file;
File dir = new File(context.getExternalFilesDir(null).getPath());
file = new File(dir, fileName + ".xls");
if (!dir.exists()) {
  dir.mkdirs();
}

如果內(nèi)存卡不存在或內(nèi)存小于1M,不進(jìn)行寫入,然后創(chuàng)建相應(yīng)的文件夾并起名字。接下來重點(diǎn)看下如何寫入excel。

生成寫入excel

導(dǎo)入相關(guān)包

這里需要導(dǎo)入jxl包,它主要就是用于處理excel的,這個(gè)包我會(huì)附在項(xiàng)目放在github中,后面會(huì)給出鏈接。

生成excel工作表

以下代碼是在指定路徑下生成excel表,此時(shí)只是一個(gè)空表。

WritableWorkbook wwb;
OutputStream os = new FileOutputStream(file);
wwb = Workbook.createWorkbook(os);

添加shee表

熟悉excel操作的都知道excel可以新建很多個(gè)sheet表。以下代碼生成第一個(gè)工作表,名字為“訂單”:

WritableSheet sheet = wwb.createSheet("訂單", 0);

添加excel表頭

添加excel的表頭,這里可以自定義表頭的樣式,先看代碼:

String[] title = { "訂單", "店名", "電話", "地址" };
Label label;
for (int i = 0; i < title.length; i++) {
  // Label(x,y,z) 代表單元格的第x+1列,第y+1行, 內(nèi)容z
        // 在Label對象的子對象中指明單元格的位置和內(nèi)容
  label = new Label(i, 0, title[i], getHeader());
  // 將定義好的單元格添加到工作表中
  sheet.addCell(label);
}

這里表頭信息我寫死了。表的一個(gè)單元格對應(yīng)一個(gè)Label,如label(0,0,”a”)代表第一行第一列所在的單元格信息為a。getHeader()是自定義的樣式,它返回一個(gè) WritableCellFormat 。來看看如何自定義樣式:

public static WritableCellFormat getHeader() {
    WritableFont font = new WritableFont(WritableFont.TIMES, 10,
        WritableFont.BOLD);// 定義字體
    try {
      font.setColour(Colour.BLUE);// 藍(lán)色字體
    } catch (WriteException e1) {
      e1.printStackTrace();
    }
    WritableCellFormat format = new WritableCellFormat(font);
    try {
      format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中
      format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中
       format.setBorder(Border.ALL, BorderLineStyle.THIN,
       Colour.BLACK);// 黑色邊框
       format.setBackground(Colour.YELLOW);// 黃色背景
    } catch (WriteException e) {
      e.printStackTrace();
    }
    return format;
}

看上面代碼就很清楚了,通過獲得WritableFont 來自定義字體的一些樣式,如顏色大小等,通過WritableCellFormat 來設(shè)置文本框的樣式,可以設(shè)置邊框底色等。具體的可以查api文檔,這里只給出例子。

添加excel內(nèi)容

for (int i = 0; i < exportOrder.size(); i++) {
      Order order = exportOrder.get(i);
      Label orderNum = new Label(0, i + 1, order.id);
      Label restaurant = new Label(1, i + 1, order.restName);
      Label nameLabel = new Label(2,i+1,order.restPhone);
      Label address = new Label(3, i + 1, order.receiverAddr);
      sheet.addCell(orderNum);
      sheet.addCell(restaurant);
      sheet.addCell(nameLabel);
      sheet.addCell(address);
      Toast.makeText(context, "寫入成功", Toast.LENGTH_LONG).show();
}

這就再簡單不過了,就是獲取order信息然后一個(gè)個(gè)寫入。

主類

這個(gè)demo布局很簡單就是一個(gè)按鈕點(diǎn)擊來生成excel,這里就不貼出來了,MainActivity的代碼如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int length = Const.OrderInfo.orderOne.length;
    for(int i = 0;i < length;i++){
      Order order = new Order( Const.OrderInfo.orderOne[i][0], Const.OrderInfo.orderOne[i][1], Const.OrderInfo.orderOne[i][2], Const.OrderInfo.orderOne[i][3]);
      orders.add(order);
    }
    btn = (Button)super.findViewById(R.id.btn);
    btn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        try {
          ExcelUtil.writeExcel(MainActivity.this,
              orders, "excel_"+new Date().toString());
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    });
}

這里我把上面生成excel的代碼封裝到一個(gè)工具類ExcelUtil中,以后使用就直接調(diào)用 。MainActivity就是將數(shù)組組裝到Order中調(diào)用ExcelUtil來寫入。到此android實(shí)現(xiàn)excel功能就實(shí)現(xiàn)了。

github源碼地址:https://github.com/reallin/Android_Excel

或者點(diǎn)擊此處本站下載。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論