示例#1
0
        /// <summary>
        /// 从 Excel 文件中读取指定工作表的数据,转换成 DataTable
        /// </summary>
        /// <param name="fileName">Excel文件名</param>
        /// <returns></returns>
        private DataTable ExcelSheet2DataTable(string fileName)
        {
            string strProcedureName =
                $"{className}.{MethodBase.GetCurrentMethod().Name}";

            WriteLog.Instance.WriteBeginSplitter(strProcedureName);

            if (!File.Exists(fileName))
            {
                string errText =
                    string.Format(
                        "没有找到文件 [{0}]。", fileName);
                WriteLog.Instance.Write(errText, strProcedureName);
                throw BuildErrorObject(-1, errText);
            }

            try
            {
                DataSet ds        = new DataSet();
                string  sheetName = GetExcelFirstSheetName(fileName);

                if (sheetName == "")
                {
                    return(null);
                }

                using (TExcelHelper excel = new TExcelHelper(fileName))
                {
                    return(excel.ExcelToDataTable(sheetName, true));
                }
            }
            catch (Exception error)
            {
                WriteLog.Instance.Write(error.Message, strProcedureName);
                throw BuildErrorObject(-1, error.Message);
            }
            finally
            {
                WriteLog.Instance.WriteEndSplitter(strProcedureName);
            }
        }
示例#2
0
        /// <summary>
        /// 从 Excel 文件中获取 Sheet 名集合,从中选择一个 Sheet 名返回
        /// </summary>
        /// <param name="fileName">Excel 文件名</param>
        /// <returns></returns>
        private string GetExcelFirstSheetName(string fileName)
        {
            string strProcedureName =
                $"{className}.{MethodBase.GetCurrentMethod().Name}";

            using (TExcelHelper excel = new TExcelHelper(fileName))
            {
                WriteLog.Instance.WriteBeginSplitter(strProcedureName);
                try
                {
                    List <string> sheetNames = excel.GetExcelSheetNames();

                    using (Dialogs.frmSelectExcelSheet selectSheet =
                               new Dialogs.frmSelectExcelSheet(sheetNames))
                    {
                        if (selectSheet.ShowDialog() == DialogResult.OK)
                        {
                            return(selectSheet.SelectSheetName);
                        }
                        else
                        {
                            return("");
                        }
                    }
                }
                catch (Exception error)
                {
                    WriteLog.Instance.Write(error.Message, strProcedureName);
                    throw BuildErrorObject(-1, error.Message);
                }
                finally
                {
                    WriteLog.Instance.WriteEndSplitter(strProcedureName);
                }
            }
        }