OpenFileDialog选择文件并获取Excel数据/开窗选择保存路径

public static System.Data.DataTable ExcelToDatatalbe(int startRow)//导入
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel文件|*.xls;*.xlsx";
            ofd.Title = "选择Excel文件";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Aspose.Cells.Workbook book = new Aspose.Cells.Workbook();
                book.Open(ofd.FileName);
                Aspose.Cells.Worksheet sheet = book.Worksheets[0];
                Cells cells = sheet.Cells;
                //获取excel中的数据保存到一个datatable中
                System.Data.DataTable dt_Import = cells.ExportDataTableAsString(startRow, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, false);
                // dt_Import.
                return dt_Import;
            }
            else
                return new System.Data.DataTable();
        }
/// <summary>
        /// 从Excel获取数据  NPOI
        /// </summary>
        /// <param name="startRow"></param>
        /// <returns></returns>
        public static System.Data.DataTable GetExcelData(int startRow, out string msg)
        {
            #region NPOI方式
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel文件|*.xls;*.xlsx";
            ofd.Title = "选择Excel文件";
            ofd.Multiselect = false;
            msg = "";
            if (ofd.ShowDialog() == DialogResult.OK)
            {

                bool hasTitle = true;
                string fileName = ofd.FileName;

                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                Sheets sheets;
                object oMissiong = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Excel.Workbook workbook = null;
                System.Data.DataTable data = new System.Data.DataTable();

                try
                {
                    if (app == null) return null;
                    workbook = app.Workbooks.Open(fileName, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong,
                        oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
                    sheets = workbook.Worksheets;

                    //将数据读入到DataTable中
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);//读取第一张表 
                    if (worksheet == null) return null;

                    int iRowCount = worksheet.UsedRange.Rows.Count;
                    int iColCount = worksheet.UsedRange.Columns.Count;
                    //生成列头
                    for (int i = 0; i < iColCount; i++)
                    {
                        var name = "column" + i;
                        if (hasTitle)
                        {
                            var txt = ((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[startRow, i + 1]).Text.ToString();
                            if (!string.IsNullOrEmpty(txt)) name = txt;
                        }
                        while (data.Columns.Contains(name)) name = name + "_1";//重复行名称会报错。
                        data.Columns.Add(new DataColumn(name, typeof(string)));
                    }
                    //生成行数据
                    Microsoft.Office.Interop.Excel.Range range;
                    int rowIdx = hasTitle ? 2 : 1;
                    for (int iRow = rowIdx; iRow <= iRowCount; iRow++)
                    {
                        DataRow dr = data.NewRow();
                        for (int iCol = 1; iCol <= iColCount; iCol++)
                        {
                            range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, iCol];
                            dr[iCol - 1] = (range.Value2 == null) ? "" : range.Text.ToString();
                        }
                        data.Rows.Add(dr);
                    }
                }

                catch (Exception ex)
                {
                    ExceptionUtil.DataCL_HTTP(ex, "", "", FileUtil.GetCurSourceFileName());
                    msg = ex.Message;
                    return null;
                }

                finally
                {
                    workbook.Close(false, oMissiong, oMissiong);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                    workbook = null;
                    app.Workbooks.Close();
                    app.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                    app = null;
                }
                return data;
            }
            #endregion
            return null;

        }
//选择文件夹
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
{
    string FolderName = folderBrowserDialog.SelectedPath;
}

 

在 Unity 中,可以使用 `EditorUtility.OpenFilePanel` 方法来打开一个弹窗选择文件,并获取选择文件路径。具体步骤如下: 1. 在 Unity 编辑器中,创建一个按钮或其他 UI 元素,用于触发选择文件的操作。 2. 在按钮或 UI 元素的点击事件中,调用 `EditorUtility.OpenFilePanel` 方法。 3. 在 `EditorUtility.OpenFilePanel` 方法中传入弹窗的标题、默认目录、默认文件名和文件类型过滤器等参数。 4. 在方法调用完成后,判断用户是否选择文件,如果选择文件,则获取选择文件路径。 下面是一个示例代码: ```c# using UnityEditor; public class FileSelectionExample { [MenuItem("Examples/Select File")] public static void SelectFile() { string title = "Select a file"; string directory = "Assets/"; string defaultName = "example.txt"; string extension = "txt"; string filter = $"Text files (*.{extension})|*.{extension}"; string filePath = EditorUtility.OpenFilePanel(title, directory, extension); if (!string.IsNullOrEmpty(filePath)) { Debug.Log($"Selected file: {filePath}"); } else { Debug.Log("No file selected"); } } } ``` 在上面的代码中,我们定义了一个名为 `SelectFile` 的静态方法,并在该方法上添加了 `MenuItem` 属性,用于在 Unity 编辑器中创建一个菜单,当用户点击该菜单时,会触发 `SelectFile` 方法。 在 `SelectFile` 方法中,我们调用了 `EditorUtility.OpenFilePanel` 方法,并传入了弹窗的标题、默认目录、默认文件名、文件类型过滤器等参数。如果用户选择文件,则会得到选择文件路径,并在控制台输出该路径。如果用户没有选择文件,则会输出 "No file selected"。 注意:`EditorUtility.OpenFilePanel` 方法只能在 Unity 编辑器中使用,不能在游戏运行时使用。如果需要在游戏运行时选择文件,可以使用 `System.Windows.Forms.OpenFileDialog` 等其他方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值