4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
新闻详情
使用Uni-Excel插件在unity中读写excel 还可通过选择目..._CSDN博客
来自 : CSDN技术社区 发布时间:2021-03-24
/// param name filePath w文件路径 /param /// param name ss 存放每一列数据的数组 可以是多个 第一个list最好为最大行数 /param public void ExcelDataWrite(string filePath, params List string [] ss) #region //if (Directory.Exists(Application.dataPath /StreamingAssets/ MyExcelName .xls ) false) // myaddress new FileStream(Application.dataPath /StreamingAssets/ MyExcelName .xls , FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); //else // myaddress new FileStream(Application.dataPath /StreamingAssets/ MyExcelName .xls , FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); #endregion //myaddress new FileStream(Application.dataPath /StreamingAssets/ MyExcelName .xls , FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); myaddress new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); HSSFWorkbook MyWorkbook new HSSFWorkbook();//创建一个空的Excel文件 HSSFSheet sheet01 (HSSFSheet)MyWorkbook.CreateSheet(MySheetName);//添加第一个工作表 //存储多列数据 数据长度可以不一样 看自己需求更改 for (int i 0; i ss[0].Count; i ) HSSFRow Row (HSSFRow)sheet01.CreateRow((short)i);//为工作表定义行 for (int a 0; a ss.Length; a ) HSSFCell cell (HSSFCell)Row.CreateCell((short)a); if (ss[a].Count i) cell.SetCellValue(ss[a][i].ToString());//给第i列添加数值 else cell.SetCellValue( ); //根据自己需求 改 //for (int i i hangNum.Length; i ) // HSSFRow Row (HSSFRow)sheet01.CreateRow((short)i);//为工作表定义行 // HSSFCell cell (HSSFCell)Row.CreateCell((short)0);//为第i行 定义列 // cell.SetCellValue(hangNum[i]);//给第i列添加数值 // if (i hang.Length) // { // HSSFCell cell02 (HSSFCell)Row.CreateCell((short)1); // cell02.SetCellValue(hang[i]); // } // else // { // HSSFCell cell02 (HSSFCell)Row.CreateCell((short)1); // cell02.SetCellValue( // } // #region[格式设置] // //Row.RowStyle MyWorkbook.CreateCellStyle();//定义行样式 // //Row.RowStyle.BorderBottom BorderStyle.Double;//更改行边界 // //cell.CellStyle MyWorkbook.CreateCellStyle();//定义单元格格式 // //cell.CellStyle.BorderRight BorderStyle.Thin;//改变一小格边界 // //cell.CellStyle.BorderBottom BorderStyle.Dashed; // //cell.CellStyle.BottomBorderColor HSSFColor.Red.Index; // //HSSFFont MyFont (HSSFFont)MyWorkbook.CreateFont();//定义字体 // 改变字体、字体高度、字体颜色、eto // //MyFont.FontName Tahoma // //MyFont.FontHeightInPoints 14; // //MyFont.Color HSSFColor.Gold.Index; // //MyFont.Boldweight (short)FontBoldWeight.Bold; // //设置单元格字体 // //cell.CellStyle.SetFont(MyFont); // #endregion MyWorkbook.Write(myaddress);//在Excel中写入数据 MyWorkbook.Close();//关闭打开的excel //读取excel文件数据 public void ExcelDataRead(string filepaths) HSSFWorkbook MyBook; //查找并打开excel文件 using (FileStream MyAddress_read new FileStream(filepaths, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) MyBook new HSSFWorkbook(MyAddress_read); //查找带有表单名称的表单 ISheet sheet_read MyBook.GetSheet(MySheetName); //表单最大行数据 sheet_read.LastRowNum for (int row 0; row sheet_read.LastRowNum; row ) //查找表行 IRow row_read sheet_read.GetRow(row); //每行最大列数据 row_read.LastCellNum; for (int cells 0; cells row_read.LastCellNum; cells ) //显示读取的excel内容 Debug.Log(row_read.GetCell(cells).ToString()); //支持阿拉伯语言 // Debug.Log(ArabicFixer.Fix(row_read.GetCell(cells).ToString())); 保存目录提示框的实现方式 基本设置
using UnityEngine;using System.Collections;using System.Runtime.InteropServices;using System;namespace Common [StructLayout(LayoutKind.Sequential, CharSet CharSet.Auto)] public class FileDlg public int structSize 0; public IntPtr dlgOwner IntPtr.Zero; public IntPtr instance IntPtr.Zero; public String filter null; public String customFilter null; public int maxCustFilter 0; public int filterIndex 0; public String file null; public int maxFile 0; public String fileTitle null; public int maxFileTitle 0; public String initialDir null; public String title null; public int flags 0; public short fileOffset 0; public short fileExtension 0; public String defExt null; public IntPtr custData IntPtr.Zero; public IntPtr hook IntPtr.Zero; public String templateName null; public IntPtr reservedPtr IntPtr.Zero; public int reservedInt 0; public int flagsEx 0; [StructLayout(LayoutKind.Sequential, CharSet CharSet.Auto)] public class OpenFileDlg : FileDlg public class OpenFileDialog [DllImport( Comdlg32.dll , SetLastError true, ThrowOnUnmappableChar true, CharSet CharSet.Auto)] public static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd); public class SaveFileDialog [DllImport( Comdlg32.dll , SetLastError true, ThrowOnUnmappableChar true, CharSet CharSet.Auto)] public static extern bool GetSaveFileName([In, Out] SaveFileDlg ofd); [StructLayout(LayoutKind.Sequential, CharSet CharSet.Auto)] public class SaveFileDlg : FileDlg
选择文件和选择文件地址
using UnityEngine;using System.Collections;using System.Collections.Generic;using System.IO;using System.Text;using Common;public class FileControllor : MonoBehaviour //文件格式根据自己需求更改 .txt .xls之类的 private void Start() //OpenProject(); // SaveProject(); //选择一个文件的地址 public void OpenProject() OpenFileDlg pth new OpenFileDlg(); pth.structSize System.Runtime.InteropServices.Marshal.SizeOf(pth); pth.filter txt (*.txt) ; pth.file new string(new char[256]); pth.maxFile pth.file.Length; pth.fileTitle new string(new char[64]); pth.maxFileTitle pth.fileTitle.Length; pth.initialDir Application.dataPath; // default path  pth.title 打开项目 ; pth.defExt txt ; pth.flags 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; //0x00080000 是否使用新版文件选择窗口 //0x00000200 是否可以多选文件 if (OpenFileDialog.GetOpenFileName(pth)) string filepath pth.file;//选择的文件路径;  Debug.Log(filepath); //将文件保存到一个地址 public void SaveProject() SaveFileDlg pth new SaveFileDlg(); pth.structSize System.Runtime.InteropServices.Marshal.SizeOf(pth); pth.filter xls (*.xls) ; pth.file new string(new char[256]); pth.maxFile pth.file.Length; pth.fileTitle new string(new char[64]); pth.maxFileTitle pth.fileTitle.Length; pth.initialDir Application.dataPath; // default path  pth.title 保存项目 ; pth.defExt xls ; pth.flags 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; if (SaveFileDialog.GetSaveFileName(pth)) string filepath pth.file;//选择的文件路径;  Debug.Log(filepath);

本文链接: http://uniexcel.immuno-online.com/view-704997.html

发布于 : 2021-03-24 阅读(0)