上海工程测量
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

689 lines
32 KiB

package com.project.survey.util;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import androidx.annotation.MainThread;
import androidx.annotation.WorkerThread;
import com.bingce.data.cache.CachedProject;
import com.bingce.file.FileOperator;
import com.bingce.surveyor.util.dialog.CustomDialog;
import com.bingce.surveyor.util.dialog.CustomInputDialog;
import com.bingce.utils.FileUtil;
import com.bingce.utils.ThreadPoolUtil;
import com.bingce.utils.Util;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.project.survey.App;
import com.project.survey.BuildConfig;
import com.project.survey.R;
import com.project.survey.dialog.CustomStyleDialog;
import com.project.survey.ui.instrument.setupstation.AddResultFormatActivity;
import com.project.survey.ui.instrument.setupstation.db.PointSurveyRecord;
import com.project.survey.ui.instrument.setupstation.db.resultformat.FormatRecord;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import blankj.utilcode.util.ToastUtils;
import blankj.utilcode.util.Utils;
import jxl.Workbook;
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;
import jxl.WorkbookSettings;
public class ExcelUtil {
public static final int ID_POINT_STAKING = 0;
public static final int ID_POINT_COORDINATE = ID_POINT_STAKING + 1;
public static final int ID_CONVER_PARAMETER = ID_POINT_COORDINATE + 1;
public static final int ID_RESULT_EXPORT_PLANE_POINT_COORDINATE = ID_CONVER_PARAMETER + 1;
public static final int ID_RESULT_EXPORT_LATLON_POINT_COORDINATE = ID_RESULT_EXPORT_PLANE_POINT_COORDINATE + 1;
public static final int ID_CUSTOM_EXPORT = ID_RESULT_EXPORT_LATLON_POINT_COORDINATE + 1;
public static final int ID_LINE_STAKING = ID_CUSTOM_EXPORT + 1;
public static final int ID_MIDDLE_SIDE_STAKING = ID_LINE_STAKING + 1;
public static final int ID_STAKING_TO_LINE = ID_MIDDLE_SIDE_STAKING + 1;
public static final int ID_STAKING_SIDE_COMMON = ID_STAKING_TO_LINE + 1;
public static final int ID_CAD_STAKING = ID_STAKING_SIDE_COMMON + 1;
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 final static List<String> topTabsPointCoordinate = Arrays.asList(
Utils.getApp().getString(R.string.point_name),
Utils.getApp().getString(R.string.point_code),
Utils.getApp().getString(R.string.latitude),
Utils.getApp().getString(R.string.longitude),
Utils.getApp().getString(R.string.altitude),
Utils.getApp().getString(R.string.antenna_type),
Utils.getApp().getString(R.string.antenna_height),
Utils.getApp().getString(R.string.easting),
Utils.getApp().getString(R.string.northing),
Utils.getApp().getString(R.string.elevation),
Utils.getApp().getString(R.string.solution_state),
Utils.getApp().getString(R.string.satellite_num),
Utils.getApp().getString(R.string.satellite_track_num),
Utils.getApp().getString(R.string.pdop),
Utils.getApp().getString(R.string.hrms),
Utils.getApp().getString(R.string.vrms),
Utils.getApp().getString(R.string.diff_delay),
Utils.getApp().getString(R.string.remark),
Utils.getApp().getString(R.string.acquisition_time),
Utils.getApp().getString(R.string.base_station) + "ID",
Utils.getApp().getString(R.string.base_station) + Utils.getApp().getString(R.string.latitude),
Utils.getApp().getString(R.string.base_station) + Utils.getApp().getString(R.string.longitude),
Utils.getApp().getString(R.string.base_station) + Utils.getApp().getString(R.string.distance),
Utils.getApp().getString(R.string.tilt_survey));
private final static List<String> topTabsResultExportPLANE = Arrays.asList(
Utils.getApp().getString(R.string.point_name),
Utils.getApp().getString(R.string.point_code),
Utils.getApp().getString(R.string.easting),
Utils.getApp().getString(R.string.northing),
Utils.getApp().getString(R.string.elevation));
private final static List<String> topTabsResultExportLatLon = Arrays.asList(
Utils.getApp().getString(R.string.point_name),
Utils.getApp().getString(R.string.point_code),
Utils.getApp().getString(R.string.latitude),
Utils.getApp().getString(R.string.longitude),
Utils.getApp().getString(R.string.altitude));
public static List<String> topTabCustomExport = Arrays.asList();
/**
* 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
*/
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文件调用此方法
*/
public static void exportFormat(int ID_TYPE, Context context, CachedProject currentProject, IExportExcel exportExcel) {
exportFormat(ID_TYPE, context, currentProject, null, exportExcel);
}
public static void exportFormat(int ID_TYPE, Context context, CachedProject currentProject, FormatRecord formatRecord, IExportExcel exportExcel) {
if (XXPermissions.isGranted(context, Permission.MANAGE_EXTERNAL_STORAGE)) {
showEditTextDialog(ID_TYPE, context, currentProject, formatRecord, exportExcel);
} else {
ToastUtils.showShort(context.getString(R.string.open_read_write_permissions));
}
}
private static CustomInputDialog.Builder inputBuilder;
private static void showEditTextDialog(int ID_TYPE, Context context, CachedProject currentProject, FormatRecord formatRecord, IExportExcel exportExcel) {
inputBuilder = new CustomInputDialog.Builder(context);
inputBuilder.setTitle(context.getString(R.string.export_excel_file_name)).setInputHint(context.getString(R.string.file_name)).setButtonConfirm(new CustomInputDialog.setOnConfirmClickListener() {
@Override
public void OnConfirmClick(String editText) {
if (TextUtils.isEmpty(editText.trim())) {
ToastUtils.showShort(context.getString(R.string.enter_file_name));
return;
}
setExcelInformation(ID_TYPE, editText.trim(), context, currentProject, formatRecord, exportExcel);
}
}).create().show();
}
public interface IExportExcel {
void onExportExcel(String filePaths);
}
/**
* 设置excel保存的path、名称
*/
private static String sheetName, filePaths;
private static <T> void setExcelInformation(int ID_TYPE, String inputFileName, Context context, CachedProject currentProject, FormatRecord formatRecord, IExportExcel exportExcel) {
filePaths = FileUtil.getSDPath() + "/" + context.getString(R.string.surveyor_exported_file) + "/" + context.getString(R.string.point_survey) + "/" + inputFileName + ".xls";
switch (ID_TYPE) {
case ID_POINT_COORDINATE:
sheetName = context.getString(R.string.coordinate_points_library) + " " + SystemUtils.getSimpleDateFormat();
break;
case ID_RESULT_EXPORT_PLANE_POINT_COORDINATE://(E,N,h).xls
filePaths = FileUtil.getSDPath() + "/" + context.getString(R.string.surveyor_exported_file) + "/" + context.getString(R.string.point_survey) + "/" + inputFileName + "(E,N,h).xls";
sheetName = context.getString(R.string.results_the_export) + " " + SystemUtils.getSimpleDateFormat();
break;
case ID_RESULT_EXPORT_LATLON_POINT_COORDINATE://(B,L,H).xls
filePaths = FileUtil.getSDPath() + "/" + context.getString(R.string.surveyor_exported_file) + "/" + context.getString(R.string.point_survey) + "/" + inputFileName + "(B,L,H).xls";
sheetName = context.getString(R.string.results_the_export) + " " + SystemUtils.getSimpleDateFormat();
break;
case ID_CUSTOM_EXPORT://自定义格式
sheetName = context.getString(R.string.results_the_export) + " " + SystemUtils.getSimpleDateFormat();
break;
}
if (isCheckExistFile(filePaths)) {
new CustomDialog.Builder(context).setContent("\"" + FileUtil.getSDPath() + "/" + context.getString(R.string.surveyor_exported_file) + "/" + context.getString(R.string.point_survey) + "/" + "\"" + context.getString(R.string.folder_name_already_exists) + "[" + inputFileName + ".xls]" + context.getString(R.string.replace_file)).setButtonConfirm(context.getString(R.string.replace), new View.OnClickListener() {
@Override
public void onClick(View v) {
ThreadPoolUtil.execute(() -> {
ExcelUtil.initExcel(filePaths, sheetName, ID_TYPE, formatRecord);
exportExcel.onExportExcel(filePaths);
});
}
}).setButtonCancel(context.getString(R.string.rename), new View.OnClickListener() {
@Override
public void onClick(View v) {
inputBuilder.create().show();
}
}).create().show();
} else {
ThreadPoolUtil.execute(() -> {
ExcelUtil.initExcel(filePaths, sheetName, ID_TYPE, formatRecord);
exportExcel.onExportExcel(filePaths);
});
}
}
/**
* 判断导出文件是否重复
*
* @param filePaths 文件路径
*/
private static boolean isCheckExistFile(String filePaths) {
File file = new File(filePaths);
return file.exists();
}
/**
* 初始化Excel表格
*
* @param filePath 存放excel文件的路径(path/demo.xls)
* @param sheetName Excel表格的表名
* @param colName excel中包含的列名(可以有多个)
*/
private static List<String> colName;
private static void initExcel(String filePath, String sheetName, int ID_TYPE, FormatRecord formatRecord) {
switch (ID_TYPE) {
case ID_POINT_COORDINATE:
//用于判断是否导出的数据是否显示表格头
if (formatRecord != null) {
List<String> topCustomTabsPointCoordinate = new ArrayList<>();
String[] split = formatRecord.format_content.split(formatRecord.divided_symbols);
for (int i = 0; i < split.length; i++) {
topCustomTabsPointCoordinate.add(split[i]);
}
colName = topCustomTabsPointCoordinate;
} else {
colName = topTabsPointCoordinate;
}
break;
case ID_RESULT_EXPORT_PLANE_POINT_COORDINATE:
colName = topTabsResultExportPLANE;
break;
case ID_RESULT_EXPORT_LATLON_POINT_COORDINATE:
colName = topTabsResultExportLatLon;
break;
case ID_CUSTOM_EXPORT:
colName = topTabCustomExport;
break;
}
format();
WritableWorkbook workbook = null;
try {
File file = new File(filePath);
if (file.getParentFile() != null && !file.getParentFile().exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
workbook = Workbook.createWorkbook(file);
//设置表格的名字
WritableSheet sheet = workbook.createSheet(sheetName, 0);
if (formatRecord != null) {
if (formatRecord.file_header) {
//创建标题栏
sheet.addCell((WritableCell) new Label(0, 0, filePath, arial14format));
for (int col = 0; col < colName.size(); col++) {
sheet.addCell(new Label(col, 0, colName.get(col), arial10format));
}
//设置行高
sheet.setRowView(0, 340);
}
} else {
//创建标题栏
sheet.addCell((WritableCell) new Label(0, 0, filePath, arial14format));
for (int col = 0; col < colName.size(); col++) {
sheet.addCell(new Label(col, 0, colName.get(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();
}
}
}
}
/**
* 导出坐标库(新版本)
*
* @param objList
* @param fileName
* @param context
*/
@WorkerThread
public static void exportCoordinator(List<PointSurveyRecord> objList, String fileName, Context context, FormatRecord formatRecord) {
// CoordinateSystem coordinateSystem = CachedCurrentCoordinateSystem.currentCoordinateSystem();
writeListToExcelNew(objList, fileName, context, formatRecord, pointBaseRecord -> {
List<String> regexList = new ArrayList<>();
String[] split = formatRecord.format_content.split(formatRecord.divided_symbols);
for (int i = 0; i < split.length; i++) {
regexList.add(split[i]);
}
List<String> list = new ArrayList<>();
for (int i = 0; i < regexList.size(); i++) {
String s = regexList.get(i);
if (s.equals("点名") || s.equals("Point name") || s.equals("№точ.")) {
list.add(pointBaseRecord.pointName);
} else if (s.equals("编码") || s.equals("Code") || s.equals("Код")) {
list.add(pointBaseRecord.code);
} else if (s.equals("纬度") || s.equals("Latitude") || s.equals("Широта")) {
list.add(convertAngleFormat(formatRecord.angle_format, pointBaseRecord.latitude));
} else if (s.equals("经度") || s.equals("Longitude") || s.equals("Долгота")) {
list.add(convertAngleFormat(formatRecord.angle_format, pointBaseRecord.longitude));
} else if (s.equals("大地高") || s.equals("Geodetic height") || s.equals("Высота")) {
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.altitude));
} else if (s.equals("天线类型") || s.equals("Antenna type") || s.equals("Тип антенны")) {
list.add(pointBaseRecord.antennaTypeName == null ? "" : pointBaseRecord.antennaTypeName);
} else if (s.equals("天线高") || s.equals("Antenna height") || s.equals("Высота антенны")) {
if (pointBaseRecord.antennaHeight == -1) {
list.add("");
} else {
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.antennaHeight));
}
} else if (s.equals("北坐标") || s.equals("Northing") || s.equals("Северные координаты.")) {
list.add(Util.formatDouble2String(pointBaseRecord.planeN, 6));
} else if (s.equals("东坐标") || s.equals("Easting") || s.equals("Восточные координаты.")) {
list.add(Util.formatDouble2String(pointBaseRecord.planeE, 6));
} else if (s.equals("高程") || s.equals("Elevation") || s.equals("Восточные координаты.")) {
list.add(Util.formatDouble2String(pointBaseRecord.planeH, 6));
} else if (s.equals("解状态") || s.equals("Solution state") || s.equals("Режим решения.")) {
list.add(pointBaseRecord.solutionState);
} else if (s.equals("解算卫星") || s.equals("Satellite number") || s.equals("Спутник для вычисления")) {
if (pointBaseRecord.satelliteNum == -1) {
list.add("");
} else {
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.satelliteNum));
}
} else if (s.equals("可见卫星") || s.equals("Satellite track num") || s.equals("Видимый спутник")) {
if (pointBaseRecord.satelliteTrackNum == -1) {
list.add("");
} else {
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.satelliteTrackNum));
}
} else if (s.equals(context.getString(R.string.pdop))) {
if (pointBaseRecord.pdop == -1) {
list.add("");
} else {
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.pdop));
}
} else if (s.equals(context.getString(R.string.hrms))) {
if (pointBaseRecord.hrms == -1) {
list.add("");
} else {
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.hrms));
}
} else if (s.equals(context.getString(R.string.vrms))) {
if (pointBaseRecord.vrms == -1) {
list.add("");
} else {
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.vrms));
}
} else if (s.equals("差分延迟") || s.equals("Diff age") || s.equals("Дифференциальная задержка")) {
if (pointBaseRecord.diffAge == -1) {
list.add("");
} else {
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.diffAge));
}
} else if (s.equals("备注") || s.equals("Remark") || s.equals("примечан")) {
list.add(pointBaseRecord.remarks);
} else if (s.equals("采集时间") || s.equals("Measure time") || s.equals("Время сбора")) {
list.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pointBaseRecord.createdAt));
} else if (s.equals("惯导测量") || s.equals("Tilt survey") || s.equals("Нормативное измерение")) {
if (pointBaseRecord.isTiltEnable) {
list.add(context.getString(R.string.yes));
} else {
list.add(context.getString(R.string.no));
}
}
}
return list;
});
}
/**
* 根据成果导出的格式,经纬度进行转换导出
*
* @param angle_format_type
* @param l
* @return
*/
private static String convertAngleFormat(int angle_format_type, double l) {
if (angle_format_type == AddResultFormatActivity.ANGLE_FORMAT_D_MS) {
//度.分秒
return Util.radianToDmsDoubleString(Math.toRadians(l), 6, false);
} else if (angle_format_type == AddResultFormatActivity.ANGLE_FORMAT_D_M_S) {
//度°分′秒″
return Util.radianToDmsString(Math.toRadians(l), 6, true);
} else if (angle_format_type == AddResultFormatActivity.ANGLE_FORMAT_D) {
//度
return Util.formatDouble2String(l, 6);
} else {
//弧度
return Util.formatDouble2String(Math.toRadians(l), 6);
}
}
/**
* 导出坐标库(老版本)
*
* @param objList
* @param fileName
* @param context
*/
@WorkerThread
public static void exportCoordinator(List<PointSurveyRecord> objList, String fileName, Context context) {
// CoordinateSystem coordinateSystem = CachedCurrentCoordinateSystem.currentCoordinateSystem();
writeListToExcel(objList, fileName, context, pointBaseRecord -> {
List<String> list = new ArrayList<>();
list.add(pointBaseRecord.pointName);
list.add(pointBaseRecord.code);
list.add(Util.formatDouble2String(pointBaseRecord.latitude, 6));
list.add(Util.formatDouble2String(pointBaseRecord.longitude, 6));
list.add(Util.formatDouble2String(pointBaseRecord.altitude, 6));
list.add(pointBaseRecord.antennaTypeName == null ? "" : pointBaseRecord.antennaTypeName);
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.antennaHeight));
list.add(Util.formatDouble2String(pointBaseRecord.planeE, 6));
list.add(Util.formatDouble2String(pointBaseRecord.planeN, 6));
list.add(Util.formatDouble2String(pointBaseRecord.planeH, 6));
list.add(pointBaseRecord.solutionState);
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.satelliteNum));
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.satelliteTrackNum));
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.pdop));
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.hrms));
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.vrms));
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.diffAge));
list.add(pointBaseRecord.remarks);
list.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(pointBaseRecord.createdAt));
list.add(pointBaseRecord.siteId == null ? "" : pointBaseRecord.siteId);
String blhLat = Util.radianToDmsDoubleString(Math.toRadians(pointBaseRecord.siteLatitude), 4, true);
String blhLon = Util.radianToDmsDoubleString(Math.toRadians(pointBaseRecord.siteLongitude), 4, true);
Log.e("radianToDmsString", blhLat + "----" + blhLon);
list.add(blhLat);
list.add(blhLon);
list.add(Util.formatDouble2StringDotAuto(pointBaseRecord.siteDistance2D));
if (pointBaseRecord.isTiltEnable) {
list.add(context.getString(R.string.yes));
} else {
list.add(context.getString(R.string.no));
}
return list;
});
}
/**
* 导出成果导出(平面坐标)
*
* @param objList
* @param fileName
* @param context
*/
public static void exportCoordinatePlane(List<PointSurveyRecord> objList, String fileName, Context context) {
ThreadPoolUtil.execute(() -> {
writeListToExcel(objList, fileName, context, planePointBaseRecord -> {
List<String> list = new ArrayList<>();
list.add(planePointBaseRecord.pointName);
list.add(planePointBaseRecord.code);
list.add(Util.formatDouble2String(planePointBaseRecord.planeE, 6));
list.add(Util.formatDouble2String(planePointBaseRecord.planeN, 6));
list.add(Util.formatDouble2String(planePointBaseRecord.planeH, 6));
return list;
});
});
}
/**
* 导出成果导出(经纬度坐标)
*
* @param objList
* @param fileName
* @param context
*/
public static void exportCoordinateLatlon(List<PointSurveyRecord> objList, String fileName, Context context) {
ThreadPoolUtil.execute(() -> {
writeListToExcel(objList, fileName, context, latlonPointBaseRecord -> {
List<String> list = new ArrayList<>();
list.add(latlonPointBaseRecord.pointName);
list.add(latlonPointBaseRecord.code);
list.add(Util.formatDouble2String(latlonPointBaseRecord.latitude, 6));
list.add(Util.formatDouble2String(latlonPointBaseRecord.longitude, 6));
list.add(Util.formatDouble2String(latlonPointBaseRecord.altitude, 6));
return list;
});
});
}
private interface IRecord2StringList<T> {
List<String> convert(T t);
}
private static File file;
private static <T> void writeListToExcel(List<T> objList, String fileName, Context context, IRecord2StringList<T> record2StringList) {
if (objList != null && objList.size() > 0) {
WritableWorkbook writebook = null;
InputStream in = null;
try {
WorkbookSettings setEncode = new WorkbookSettings();
setEncode.setEncoding(UTF8_ENCODING);
file = new File(fileName);
in = new FileInputStream(file);
Workbook workbook = Workbook.getWorkbook(in);
writebook = Workbook.createWorkbook(file, workbook);
WritableSheet sheet = writebook.getSheet(0);
for (int j = 0; j < objList.size(); j++) {
List<String> list = record2StringList.convert(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) == null) {
//设置列宽
sheet.setColumnView(i, 8);
} else {
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();
ToastUtils.showShort(e.getMessage());
} finally {
if (writebook != null) {
try {
writebook.close();
ThreadPoolUtil.executeInMain(new Runnable() {
@Override
public void run() {
FileUtil.scanFile(context, file);
showCompleteDialog(context, file);
}
});
} catch (Exception e) {
e.printStackTrace();
ToastUtils.showShort(e.getMessage());
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
ToastUtils.showShort(e.getMessage());
}
}
}
}
}
private static <T> void writeListToExcelNew(List<T> objList, String fileName, Context context, FormatRecord formatRecord, IRecord2StringList<T> record2StringList) {
if (objList != null && objList.size() > 0) {
WritableWorkbook writebook = null;
InputStream in = null;
int x;
try {
WorkbookSettings setEncode = new WorkbookSettings();
setEncode.setEncoding(UTF8_ENCODING);
file = new File(fileName);
in = new FileInputStream(file);
Workbook workbook = Workbook.getWorkbook(in);
writebook = Workbook.createWorkbook(file, workbook);
WritableSheet sheet = writebook.getSheet(0);
for (int j = 0; j < objList.size(); j++) {
List<String> list = record2StringList.convert(objList.get(j));
//用于判断是否导出的数据是否显示表格头 x = j + 1;代表要表格头,x = j;代表不要表格头
if (formatRecord != null) {
if (formatRecord.file_header) {
x = j + 1;
} else {
x = j;
}
} else {
x = j + 1;
}
for (int i = 0; i < list.size(); i++) {
sheet.addCell(new Label(i, x, list.get(i), arial12format));
if (list.get(i) == null) {
//设置列宽
sheet.setColumnView(i, 8);
} else {
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(x, 350);
}
writebook.write();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
ToastUtils.showShort(e.getMessage());
} finally {
if (writebook != null) {
try {
writebook.close();
ThreadPoolUtil.executeInMain(() -> {
FileUtil.scanFile(context, file);
showCompleteDialog(context, file);
});
} catch (Exception e) {
e.printStackTrace();
ToastUtils.showShort(e.getMessage());
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
ToastUtils.showShort(e.getMessage());
}
}
}
}
}
@MainThread
public static void showCompleteDialog(Context context, File file) {
new CustomStyleDialog.Builder(context)
.setTitle(context.getString(R.string.export_successful))
.setContent(file.getPath())
.setButtonSample(context.getString(R.string.close))
.setButtonCancel(context.getString(R.string.open), v -> FileUtil.openFile(context, file))
.setButtonConfirm(context.getString(R.string.send_to), v -> FileOperator.shareFile(context, file, BuildConfig.APPLICATION_ID))
.create()
.show();
}
}