局域网视频网站建设,室内设计师联盟账号,微信外卖小程序加盟,网站登录页面html模板Unity3D特效百例案例项目实战源码Android-Unity实战问题汇总游戏脚本-辅助自动化Android控件全解手册再战Android系列Scratch编程案例软考全系列Unity3D学习专栏蓝桥系列ChatGPT和AIGC
#x1f449;关于作者 专注于Android/Unity和各种游戏开发技巧#xff0c;以及各种资源分…Unity3D特效百例案例项目实战源码Android-Unity实战问题汇总游戏脚本-辅助自动化Android控件全解手册再战Android系列Scratch编程案例软考全系列Unity3D学习专栏蓝桥系列ChatGPT和AIGC
关于作者 专注于Android/Unity和各种游戏开发技巧以及各种资源分享网站、工具、素材、源码、游戏等 有什么需要欢迎底部卡片私我交流让学习不再孤单。 实践过程
Apache POI 是 Java 领域最完善的 Office 文件读写库
依赖包前置条件poi-scratchpadpoipoi-ooxmlpoi, poi-ooxml-schemaspoi-ooxml-schemasxmlbeanspoi-examplespoi, poi-scratchpad, poi-ooxmlooxml-schemasxmlbeans
类型文件类型POIFSOLE2 FilesystemHPSFOLE2 Property SetsHSSFExcel XLSHSLFPowerPoint PPTHWPFWord DOCHDGFVisio VSDHPBFPublisher PUBHSMFOutlook MSGOpenXML4JOOXMLXSSFExcel XLSXXSLFPowerPoint PPTXXWPFWord DOCXCommon SSExcel XLS and XLSX
当只要使用xls格式时、只要导入 POI 即可 当还要使用xlsx格式、还要导入 poi-ooxml 当需要操作 word、ppt、viso、outlook 等时需要用到 poi-scratchpad。
同样适用于 Android 领域但是由于 JDK 和 AndroidSDK 在源码上存在差异所以原版的 Apache POI 库并不能直接在安卓上使用
两个Jar包poi-android.jar 和 poi-ooxml-schemas.jar 这里我们用的是修改和精简过后适合安卓的版本。POI 是3.12版本的它不是 Apache 官方 POI 包截止2023年11月我仍然在使用该 Jar 包开发应用并且没有遇到什么问题。 需要 Jar 包的欢迎文章最底部卡片 Call 我。
dependencies {implementation files(libs\\poi-3.12-android-a.jar)implementation files(libs\\poi-ooxml-schemas-3.12-20150511-a.jar)
}文档 API 可以参考这里特别注意我们使用的 POI 包不是 Apache 软件基金会的部分方法及其功能会有所不同
API
HSSF 提供读写Microsoft Excel格式档案的功能。XSSF 提供读写Microsoft Excel OOXML格式档案的功能。HWPF 提供读写Microsoft Word格式档案的功能。HSLF 提供读写Microsoft PowerPoint格式档案的功能。HDGF 提供读写Microsoft Visio格式档案的功能。
HSSF 用来操作Office 2007版本前excel.xls文件XSSF用来操作Office 2007版本后的excel.xlsx文件。HSSF在org.apache.poi.hssf.usermodel包中实现Workbook 接口常用组件 HSSFWorkbook excel的文档对象 HSSFSheet excel的表单 HSSFRow excel的行 HSSFCell excel的格子单元 HSSFFont excel字体 HSSFDataFormat 日期格式 HSSFHeader sheet头 HSSFFooter sheet尾只有打印的时候才能看到效果 HSSFCellStyle cell样式 HSSFDateUtil 日期 HSSFPrintSetup 打印 HSSFErrorConstants 错误信息表
XSSF 在org.apache.xssf.usemodel包实现Workbook接口常用组件 XSSFWorkbook excel的文档对象 XSSFSheet excel的表单 XSSFRow excel的行 XSSFCell excel的格子单元 XSSFFont excel字体 XSSFDataFormat 日期格式 和HSSF类似;
workbook new XSSFWorkbook(inputStream); //创建.xlsx格式文件对象
workbook new HSSFWorkbook(inputStream); //创建.xls格式文件对象
Sheet sheetAt workbook.getSheetAt(0); //获取工作表的对象
Row row sheetAt.getRow(0); //获取工作表的行
int physicalNumberOfCells row.getPhysicalNumberOfCells(); //获取实际单元格数
Row.getCell(i) //获取工作表的单元格
Cell.getCellType() //获得单元格格式Cell.getBooleanCellValue();//获取布尔类型的单元格
Cell.getNumericCellValue();//获取数字类型的单元格
Cell.getDateCellValue();//获取日期类型的单元格
Cell.getNumericCellValue();//获取数值类型的单元格
Cell.getStringCellValue();//获取字符串类型的单元格
SheetAt.getPhysicalNumberOfRows(); //获取实际行数//创建工作表的名字
XSSFSheet sheet workbook.createSheet(WorkbookUtil.createSafeSheetName(Sheet1));Row row sheet.createRow(int i); // 创建行
Cell cell row.createCell(int i); // 创建列
Cell.setCellValue((String) map.get(j)); // 将需要添加到Excel的文本添加到对应的Cell
//将数据写入文件并保存在指定文件夹
OutputStream outputStream context.getContentResolver().openOutputStream(Uri uri);
XSSFWorkbook.write(outputStream);//读取Excel并将其写入数据库
public ListMapInteger, Object readExcel(Context context, Uri fileUri, String strFileUri) {mySQLHelp new MySQLHelp(context, mydb.db, null, 1);SQLiteDatabase writableDatabase mySQLHelp.getWritableDatabase();excelStr strFileUri.substring(strFileUri.lastIndexOf(.));try {inputStream context.getContentResolver().openInputStream(fileUri);if (excelStr.equals(.xlsx)) workbook new XSSFWorkbook(inputStream);else if (excelStr.equals(.xls)) workbook new HSSFWorkbook(inputStream);else workbook null;if (workbook ! null) {Sheet sheetAt workbook.getSheetAt(0);Row row sheetAt.getRow(0);int physicalNumberOfCells row.getPhysicalNumberOfCells();//获取实际单元格数MapInteger, Object map new HashMap();for (int i 0; i physicalNumberOfCells; i) {//将标题存储到mapObject cellFormatValue getCellFormatValue(row.getCell(i));map.put(i, cellFormatValue);}dataList.add(map);int physicalNumberOfRows sheetAt.getPhysicalNumberOfRows();//获取最大行数int size map.size();//获取最大列数contentValues new ContentValues();for (int i 1; i physicalNumberOfRows; i) {MapInteger, Object map1 new HashMap();Row row1 sheetAt.getRow(i);if (!row1.equals(null)) {for (int j 0; j size; j) {Object cellFormatValue getCellFormatValue(row1.getCell(j));map1.put(j, cellFormatValue);System.out.println(j);}contentValues.put(materialID, (String) map1.get(0));contentValues.put(materialEncoding, (String) map1.get(1));contentValues.put(materialName, (String) map1.get(2));contentValues.put(materialModel, (String) map1.get(3));contentValues.put(materialSize, (String) map1.get(4));contentValues.put(unit, (String) map1.get(5));contentValues.put(price, (String) map1.get(6));contentValues.put(count, (String) map1.get(7));contentValues.put(manufacturers, (String) map1.get(8));contentValues.put(type, (String) map1.get(9));contentValues.put(receiptor, (String) map1.get(10));contentValues.put(storagelocation, (String) map1.get(11));contentValues.put(materialState, (String) map1.get(12));writableDatabase.insert(module, null, contentValues);} else break;dataList.add(map1);}contentValues.clear();writableDatabase.close();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return dataList;}private static Object getCellFormatValue(Cell cell) {Object cellValue;if (cell ! null) {switch (cell.getCellType()) {case Cell.CELL_TYPE_BOOLEAN:cellValue cell.getBooleanCellValue();break;case Cell.CELL_TYPE_NUMERIC:cellValue String.valueOf(cell.getNumericCellValue());break;case Cell.CELL_TYPE_FORMULA:if (DateUtil.isCellDateFormatted(cell)) {cellValue cell.getDateCellValue();} else {cellValue cell.getNumericCellValue();}break;case Cell.CELL_TYPE_STRING:cellValue cell.getStringCellValue();break;default:cellValue ;}} else {cellValue ;}return cellValue;}
//读取数据库数据将其写入Excel并保存到指定路径文件夹
public void getDataAndSave(Context context,Uri uri) {ArrayListMapInteger,Object arrayList new ArrayList();MapInteger,Object m new HashMap();m.put(0,物料ID);m.put(1,物料编码);m.put(2,名称);m.put(3,编号);m.put(4,规格);m.put(5,单位);m.put(6,单价);m.put(7,数量);m.put(8,厂家);m.put(9,类别);m.put(10,经手人);m.put(11,存放地点);m.put(12,状态);arrayList.add(m);mySQLHelp new MySQLHelp(context, mydb.db, null, 1);SQLiteDatabase readableDatabase mySQLHelp.getReadableDatabase();cursor readableDatabase.rawQuery(select * from module, null);while (cursor.moveToNext()) {MapInteger,Object map new HashMap();String materialID cursor.getString(cursor.getColumnIndex(materialID));String materialEncoding cursor.getString(cursor.getColumnIndex(materialEncoding));String materialName cursor.getString(cursor.getColumnIndex(materialName));String materialModel cursor.getString(cursor.getColumnIndex(materialModel));String materialSize cursor.getString(cursor.getColumnIndex(materialSize));String unit cursor.getString(cursor.getColumnIndex(unit));String price cursor.getString(cursor.getColumnIndex(price));String count cursor.getString(cursor.getColumnIndex(count));String manufacturers cursor.getString(cursor.getColumnIndex(manufacturers));String type cursor.getString(cursor.getColumnIndex(type));String receiptor cursor.getString(cursor.getColumnIndex(receiptor));String storagelocation cursor.getString(cursor.getColumnIndex(storagelocation));String materialState cursor.getString(cursor.getColumnIndex(materialState));map.put(0,materialID);map.put(1,materialEncoding);map.put(2,materialName);map.put(3,materialModel);map.put(4,materialSize);map.put(5,unit);map.put(6,price);map.put(7,count);map.put(8,manufacturers);map.put(9,type);map.put(10,receiptor);map.put(11,storagelocation);map.put(12,materialState);arrayList.add(map);}try {XSSFWorkbook workbook new XSSFWorkbook();XSSFSheet sheet workbook.createSheet(WorkbookUtil.createSafeSheetName(Sheet1));Cell cell;int size arrayList.get(0).size();for (int i 0;i arrayList.size();i){Row row sheet.createRow(i);MapInteger, Object map arrayList.get(i);for (int j 0;j size;j){cell row.createCell(j);cell.setCellValue((String) map.get(j));}}OutputStream outputStream context.getContentResolver().openOutputStream(uri);workbook.write(outputStream);outputStream.flush();outputStream.close();Toast.makeText(context, 另存成功, Toast.LENGTH_SHORT).show();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}其他 作者小空和小芝中的小空 转载说明-务必注明来源https://zhima.blog.csdn.net/ 这位道友请留步☁️我观你气度不凡谈吐间隐隐有王者霸气日后定有一番大作为旁边有点赞收藏今日传你点了吧未来你成功☀️我分文不取若不成功⚡️也好回来找我。 温馨提示点击下方卡片获取更多意想不到的资源。 文章转载自: http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.nwjzc.cn.gov.cn.nwjzc.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.pghfy.cn.gov.cn.pghfy.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.kdxzy.cn.gov.cn.kdxzy.cn http://www.morning.jydky.cn.gov.cn.jydky.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.tqbw.cn.gov.cn.tqbw.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.ljdjn.cn.gov.cn.ljdjn.cn http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.njnqn.cn.gov.cn.njnqn.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.xjkfb.cn.gov.cn.xjkfb.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.pwdgy.cn.gov.cn.pwdgy.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.hmgqy.cn.gov.cn.hmgqy.cn http://www.morning.wtwhj.cn.gov.cn.wtwhj.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.kfwrq.cn.gov.cn.kfwrq.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.pqnps.cn.gov.cn.pqnps.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.tnjkg.cn.gov.cn.tnjkg.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.rmltt.cn.gov.cn.rmltt.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn