第一步:添加依賴包
implementation group: 'net.sourceforge.jexcelapi', name: 'jxl', version: '2.6.12'
第二步:還是直接上代碼吧!這個工具類復(fù)制過去直接用
package com.ee.activity;
import android.content.Context;
import android.widget.Toast;
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 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;
/**
* @author dmrfcoder
* @date 2018/8/9
*/
class ExcelUtil {
private static WritableFont arial14font = null;
private static WritableCellFormat arial14format = null;
private static WritableFont arial10font = null;
private static WritableCellFormat arial10format = null;
private static WritableFont arial12font = null;
private static WritableCellFormat arial12format = null;
private final static String UTF8_ENCODING = "UTF-8";
/**
* 單元格的格式設(shè)置 字體大小 顏色 對齊方式、背景顏色等...
*/
private static void format() {
try {
arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
arial14font.setColour(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(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);
//設(shè)置邊框
arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
} catch (WriteException e) {
e.printStackTrace();
}
}
/**
* 初始化Excel
*
* @param fileName 導(dǎo)出excel存放的地址(目錄)
* @param colName excel中包含的列名(可以有多個)
*/
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);
//設(shè)置表格的名字
WritableSheet sheet = workbook.createSheet("賬單", 0);
//創(chuàng)建標(biāo)題欄
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));
}
//設(shè)置行高
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);
for (int j = 0; j < objList.size(); j++) {
ProjectBeanOne projectBeanOne = (ProjectBeanOne) objList.get(j);
List<String> list = new ArrayList<>();
list.add(projectBeanOne.getTemplateName());
list.add(projectBeanOne.getBlockNum());
for (int i = 0; i < list.size(); i++) {
sheet.addCell(new Label(i, j + 1, list.get(i), arial12format));
if (list.get(i).length() <= 4) {
//設(shè)置列寬
sheet.setColumnView(i, list.get(i).length() + 8);
} else {
//設(shè)置列寬
sheet.setColumnView(i, list.get(i).length() + 5);
}
}
//設(shè)置行高
sheet.setRowView(j + 1, 350);
}
writebook.write();
Toast.makeText(c, "導(dǎo)出Excel成功", 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 void export(){
projectBeans = GreenDaoUtils.queryProject();
LogUtils.Loge("XmlParserUtils","export===" + projectBeans.size());
String filePath = Environment.getExternalStorageDirectory() + "/AndroidExcelDemo";
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
String excelFileName = "/demo.xls";
String[] title = {"名字", "年齡"};
String sheetName = "demoSheetName";
filePath = filePath + excelFileName;
ExcelUtil.initExcel(filePath,title);
ExcelUtil.writeObjListToExcel(projectBeans, filePath, this);
}
ENDGń场!驹溃!