2018-03-31

package com.jokey.wisdomaldetection.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.jokey.wisdomaldetection.R;
import com.jokey.wisdomaldetection.teble.TestDataTable;

import org.litepal.crud.DataSupport;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * Created by Administrator on 2018/3/27.
 */

public class WriteToExcelUtils {

    private static List<TestDataTable> DataList;

    public static void saveFile(String fileName, Context context, int rawid) throws IOException {

        // 首先判斷該目錄下的文件夾是否存在
        File dir = new File(Environment.getExternalStorageDirectory() + "/Wisdom/");

        if (!dir.exists()) {
            // 文件夾不存在 , 則創(chuàng)建文件夾
            dir.mkdirs();
        }

        // 判斷目標(biāo)文件是否存在
        File file1 = new File(dir, fileName);

        if (!file1.exists()) {
            file1.createNewFile(); // 創(chuàng)建文件

        }
        // 開始進(jìn)行文件的復(fù)制
        InputStream input = context.getResources().openRawResource(rawid); // 獲取資源文件raw
        // 標(biāo)號(hào)
        try {

            FileOutputStream out = new FileOutputStream(file1); // 文件輸出流、用于將文件寫到SD卡中
            // -- 從內(nèi)存出去
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = (input.read(buffer))) != -1) { // 讀取文件,-- 進(jìn)到內(nèi)存

                out.write(buffer, 0, len); // 寫入數(shù)據(jù) ,-- 從內(nèi)存出
            }

            input.close();
            out.close(); // 關(guān)閉流
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void printer(Context context) {
        try {
            saveFile("wisdom.xls", context, R.raw.wisdom);//文件目錄res/raw


            String wisdomExcel = Environment.getExternalStorageDirectory() + "/wisdom/wisdom.xls";

            // 打開文件
            WritableWorkbook workbook = Workbook.createWorkbook(new File(wisdomExcel));

            Set date = new HashSet();

            DataList = DataSupport.findAll(TestDataTable.class);


            WritableSheet[] sheet=new WritableSheet[20];

            for(int i=0;i<DataList.size();i++){
                date.add(DataList.get(i).getMt().substring(0,11));
            }
            Iterator it = date.iterator();

            int k=0;

            while (it.hasNext()){
                sheet[k]=workbook.createSheet(String.valueOf(it.next()),k++);
            }


            /**
             * 自定義單元格
             * 定義格式 字體 下劃線 斜體 粗體 顏色
             */
            WritableFont wf_title = new WritableFont(WritableFont.TIMES, 20, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);

            WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 單元格定義
            wcf_title.setBackground(jxl.format.Colour.BLACK); // 設(shè)置單元格的背景顏色
            wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 設(shè)置對(duì)齊方式
            //wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.MEDIUM, Colour.BLACK);


            /**
             * 自定義單元格
             * 定義格式 字體 下劃線 斜體 粗體 顏色
             */
            WritableFont wf_center = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);

            WritableCellFormat wcf_center = new WritableCellFormat(wf_center); // 單元格定義
            wcf_center.setBackground(jxl.format.Colour.BLACK); // 設(shè)置單元格的背景顏色
            wcf_center.setAlignment(jxl.format.Alignment.CENTRE); // 水平居中
            wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE);//豎直居中

            wcf_center.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.MEDIUM, Colour.BLACK);





            /**
             * 添加基本樣式
             */
            // 1.添加Label對(duì)象三個(gè)參數(shù)意思:【列,行,值】
            sheet[0].addCell(new Label(0, 0, "導(dǎo)桿壓降測(cè)量結(jié)果", wcf_title)); // 普通的帶有定義格式的單元格
            sheet[0].mergeCells(0, 0, 12, 1); // 標(biāo)題合并單元格


            //添加日期
            sheet[0].addCell(new Label(1,2,"2018-3-31",wcf_center));//模擬插入槽號(hào)數(shù)值
            sheet[0].addCell(new Label(0,2,"測(cè)量日期",wcf_center));
            sheet[0].mergeCells(1, 2, 12, 2); // 槽號(hào)合并單元格


           //添加槽號(hào)
            sheet[0].addCell(new Label(1,3,"5120",wcf_center));//模擬插入槽號(hào)數(shù)值
            sheet[0].addCell(new Label(0,3,"槽號(hào)",wcf_center));
            sheet[0].mergeCells(1, 3, 12, 3); // 槽號(hào)合并單元格


            sheet[0].addCell(new Label(0,4,"A面",wcf_center));
            sheet[0].mergeCells(0, 4, 0, 5); // A面合并單元格
            for(int i=0;i<12;i++){
                sheet[0].addCell(new Label(i+1,4,""+(i+1),wcf_center));
                //模擬插入數(shù)據(jù)
                sheet[0].addCell(new Label(i+1,4+1,""+(i+500),wcf_center));

            }

            sheet[0].addCell(new Label(0,6,"B面",wcf_center));
            sheet[0].mergeCells(0, 6, 0, 7); // B面合并單元格
            for(int i=0;i<12;i++){
                sheet[0].addCell(new Label(i+1,6,""+(i+1),wcf_center));
                //模擬插入數(shù)據(jù)
                sheet[0].addCell(new Label(i+1,6+1,""+(i+500),wcf_center));

            }









            // 寫入數(shù)據(jù)并關(guān)閉文件
            workbook.write();
            workbook.close();

            //Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
            Log.i("jokeyWriteToExcel","Excel---保存成功");
            //以下代碼是連接打印機(jī)的,這邊沒有用到,所以可以忽略


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("jokeyWriteToExcel","excel失敗---"+e.toString());

        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容