导出excel 工具类
一、android导出Excel使用什么库
implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
二,导出工具类
import android.content.Context;
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;
public 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";
/**
* 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
*/
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);
//设置边框
arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
} catch (WriteException e) {
e.printStackTrace();
}
}
/**
* 初始化Excel表格
*
* @param filePath 存放excel文件的路径(path/demo.xls)
* @param sheetName Excel表格的表名
* @param colName excel中包含的列名(可以有多个)
*/
public static void initExcel(String filePath, String sheetName, String[] colName) {
format();
WritableWorkbook workbook = null;
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
} else {
return;
}
workbook = Workbook.createWorkbook(file);
//设置表格的名字
WritableSheet sheet = workbook.createSheet(sheetName, 0);
//创建标题栏
sheet.addCell((WritableCell) new Label(0, 0, filePath, 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();
}
}
}
}
/**
* 将制定类型的List写入Excel中
*
* @param objList 待写入的list
* @param fileName
* @param mContext
* @param <T>
* public int box_name;
* public String key_rfid;
* public int status;
* public int user_id;
*
* public String uptime;
*/
@SuppressWarnings("unchecked")
public static <T> void writeObjListToExcel(List<T> objList, String fileName, Context mContext) {
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++) {
StandingBookTable demoBean = (StandingBookTable) objList.get(j);
List<String> list = new ArrayList<>();
list.add(demoBean.box_name + "号格子");
list.add(demoBean.key_rfid);
if (demoBean.status == 1) {
list.add("取出");
} else {
list.add("存入");
}
list.add(demoBean.user_id);
list.add(demoBean.uptime);
list.add(demoBean.ctime);
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) {
//设置列宽
sheet.setColumnView(i, list.get(i).length() + 8);
} else {
//设置列宽
sheet.setColumnView(i, list.get(i).length() + 5);
}
}
//设置行高
sheet.setRowView(j + 1, 350);
}
writebook.write();
workbook.close();
} 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();
}
}
}
}
}
}
三、查询db文件直接导出。
public void downloadExcel(Callback successBack){
String path = Environment.getExternalStorageDirectory().getPath()+"/documentFile";
String excelFileName = "/存取记录.xls";
String[] title = {"柜子号", "rfid", "使用状态", "用户id", "操作时间", "创建时间"};
String sheetName = "Sheet1";
ArrayList<StandingBookTable> standingBookTables = new ArrayList<>();
SQLiteDatabase db = SQLiteDBUtil.openOrCreateDatabase(myContext,"myContext.db");
Cursor cursor = db.rawQuery("select box_name,key_rfid,status,user_id,uptime,ctime from use_record;",null);
String a ="";
if (cursor.moveToFirst()) {
do {
int i = 0;
StandingBookTable demoBean = new StandingBookTable();
demoBean.box_name=cursor.getString(i++);
demoBean.key_rfid=cursor.getString(i++);
demoBean.status= Integer.parseInt(cursor.getString(i++));
demoBean.user_id=cursor.getString(i++);
demoBean.uptime=cursor.getString(i++);
demoBean.ctime=cursor.getString(i++);
standingBookTables.add(demoBean);
} while (cursor.moveToNext());
}
cursor.close();
String filePaths = path + excelFileName;
File file = new File(filePaths);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
ExcelUtil.initExcel(filePaths, sheetName, title);
ExcelUtil.writeObjListToExcel(standingBookTables, filePaths, myContext);
successBack.invoke("导出成功");
}