zoukankan      html  css  js  c++  java
  • Android使用jxl快速生成Excel表

    super.onCreate();
            
    
    
            //模拟数据集合
            students = new ArrayList<Strudent>();
            for (int i = 1; i <= 10; i++) { 
                students.add(new Strudent("小红"+i,"女","1"+i)); 
            }
            exportExcel(this);
    /** * 导出excel * @param view */
        public void exportExcel(App app) { 
            file = new File(getSDPath() + "/Record"); 
            makeDir(file); 
            ExcelUtil.initExcel(file.toString() + "/成绩表.xls", title); 
            fileName = getSDPath() + "/Record/成绩表.xls"; 
            System.out.println("=======================" + fileName);
            ExcelUtil.writeObjListToExcel(getRecordData(), fileName, this); 
            }
    
    
    /** * 将数据集合 转化成ArrayList<ArrayList<String>> * @return */
        private ArrayList<ArrayList<String>> getRecordData() { 
            recordList = new ArrayList(); 
            for (int i = 0; i <students.size(); i++) { 
                Strudent student = students.get(i); 
                ArrayList<String> beanList = new ArrayList<String>();
                beanList.add(student.id); 
                beanList.add(student.name);
                beanList.add(student.sex); 
                recordList.add(beanList); 
                } 
            return recordList; 
            } 
        
    
    
        private String getSDPath() {
            File sdDir = null; 
            boolean sdCardExist = Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); 
            if (sdCardExist) { 
                sdDir = Environment.getExternalStorageDirectory(); 
            } 
            String dir = sdDir.toString(); 
            return dir; 
            } 
    
    
    
        public void makeDir(File dir) {
            if (!dir.getParentFile().exists()) {
                makeDir(dir.getParentFile()); 
                } 
            dir.mkdir(); 
            }

    https://www.jianshu.com/p/d3d40a69a9b1

    package com.hg.excel;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import android.content.Context;
    import android.widget.Toast;
    
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.format.Colour;
    import jxl.write.Label;
    import jxl.write.WritableCell;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    
    public class ExcelUtil {
        public static WritableFont arial14font = null; 
        public static WritableCellFormat arial14format = null; 
        public static WritableFont arial10font = null; 
        public static WritableCellFormat arial10format = null; 
        public static WritableFont arial12font = null; 
        public static WritableCellFormat arial12format = null; 
        public final static String UTF8_ENCODING = "UTF-8"; 
        public final static String GBK_ENCODING = "GBK";
        /** * 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等... */ 
        public static void format() { 
            try { 
                arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD); 
                arial14font.setColour(jxl.format.Colour.LIGHT_BLUE); 
                arial14format = new WritableCellFormat(arial14font); 
                arial14format.setAlignment(jxl.format.Alignment.CENTRE); 
                arial14format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); 
                arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW); 
                arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD); 
                arial10format = new WritableCellFormat(arial10font); 
                arial10format.setAlignment(jxl.format.Alignment.CENTRE); 
                arial10format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); 
                arial10format.setBackground(Colour.GRAY_25); 
                arial12font = new WritableFont(WritableFont.ARIAL, 10); 
                arial12format = new WritableCellFormat(arial12font); 
                arial10format.setAlignment(jxl.format.Alignment.CENTRE);//对齐格式 
                arial12format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); //设置边框 
                } catch (WriteException e) {
                    e.printStackTrace(); 
                    } 
            } 
        /** * 初始化Excel * @param fileName * @param colName */ 
        public static void initExcel(String fileName, String[] colName) 
        { 
            format(); 
            WritableWorkbook workbook = null; 
            try { File file = new File(fileName); 
            if (!file.exists()) {
                file.createNewFile(); 
                } 
            workbook = Workbook.createWorkbook(file); 
            WritableSheet sheet = workbook.createSheet("成绩表", 0); 
            //创建标题栏 
            sheet.addCell((WritableCell) new Label(0, 0, fileName,arial14format)); 
            for (int col = 0; col < colName.length; col++) {
                sheet.addCell(new Label(col, 0, colName[col], arial10format)); 
                } 
            sheet.setRowView(0,340); 
            //设置行高 
    workbook.write();
    } catch (Exception e) { e.printStackTrace(); } finally { if (workbook != null) { try { workbook.close(); } catch (Exception e) { e.printStackTrace(); } } } } @SuppressWarnings("unchecked") public static <T> void writeObjListToExcel(List<T> objList,String fileName, Context c) { if (objList != null && objList.size() > 0) { WritableWorkbook writebook = null; InputStream in = null; try { WorkbookSettings setEncode = new WorkbookSettings(); setEncode.setEncoding(UTF8_ENCODING); in = new FileInputStream(new File(fileName)); Workbook workbook = Workbook.getWorkbook(in); writebook = Workbook.createWorkbook(new File(fileName),workbook); WritableSheet sheet = writebook.getSheet(0); // sheet.mergeCells(0,1,0,objList.size()); //合并单元格 // sheet.mergeCells() for (int j = 0; j < objList.size(); j++) { ArrayList<String> list = (ArrayList<String>) objList.get(j); for (int i = 0; i < list.size(); i++) { sheet.addCell(new Label(i, j + 1, list.get(i),arial12format)); if (list.get(i).length() <= 5){ sheet.setColumnView(i,list.get(i).length()+8); //设置列宽 }else { sheet.setColumnView(i,list.get(i).length()+5); //设置列宽 } } sheet.setRowView(j+1,350); //设置行高 } writebook.write(); Toast.makeText(c, "导出到手机存储中文件夹Record成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } finally { if (writebook != null) { try { writebook.close(); } catch (Exception e) { e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } // } // } // } // } // } // // // // //}
        private ArrayList<ArrayList<String>> recordList; 
        private List<Strudent> students; 
        private static String[] title = { "编号","姓名","性别","年龄","班级","数学","英语","语文" }; 
        private File file; 
        private String fileName;
  • 相关阅读:
    nginx反向代理编译异常
    TCP/ip协议栈之内核调优
    Tcp之异常
    Codeforces Round #584
    Codeforces Round #588 (Div. 2)
    Codeforces Round #587 (Div. 3) F
    P4587 [FJOI2016]神秘数 主席树
    P4559 [JSOI2018]列队 主席树
    P4098 [HEOI2013]ALO 可持久化01trie
    4771: 七彩树 主席树
  • 原文地址:https://www.cnblogs.com/BelieveFish/p/11113558.html
Copyright © 2011-2022 走看看