예제 #1
0
        /// <summary>
        /// 导入excel单个表单
        /// </summary>
        /// <param name="excel">MoqikakaExcel对象</param>
        /// <param name="sheetIndex">导入表单序号</param>
        /// <param name="dbTables">数据库表名集合</param>
        private static void ImportSheet(MoqikakaExcel excel, Int32 sheetIndex, List<String> dbTables)
        {
            String sheetName = excel.GetSheetName(sheetIndex);

            //排除没用的表单/已忽略表单/数据库不存在的表单
            if (ExcelBLL.IsUselessSheet(sheetName)
                || IgnoreSheetsBLL.IsIgnoreSheet(sheetName)
                || !dbTables.Contains(sheetName.ToLower()))
                return;

            //读取表单数据
            DataTable table = ExcelBLL.TryRead(excel, sheetIndex);
            if (table == null)
            {
                resultInfo[sheetName] = 0;
                return;
            }

            try
            {
                List<String> sqlList = ExcelBLL.GetSQL(table);

                Int32 rows = ExcelBLL.Insert(sqlList, table.TableName, excel.Path, false);

                resultInfo[table.TableName] = rows;
            }
            catch (Exception ex)
            {
                resultInfo[sheetName] = 0;
                Trace.Write("错误表单: " + sheetName + "\r\n" + "错误信息: " + ex.Message + "\r\n" + ex.StackTrace);
            }
        }
예제 #2
0
        /// <summary>
        /// 读取表单数据
        /// </summary>
        /// <param name="excel">文档对象</param>
        /// <param name="sheetIndex">表单序号</param>
        /// <returns>表单数据</returns>
        public static DataTable TryRead(MoqikakaExcel excel, Int32 sheetIndex)
        {
            DataTable table = null;

            try
            {
                //读取表单数据
                table = excel.ReadAt(sheetIndex);
            }
            catch (Exception ex)
            {
                //记录异常日志信息
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("【表单读取异常】");
                sb.AppendLine("异常文件: " + excel.Path);
                sb.AppendLine("异常表单: " + excel.GetSheetName(sheetIndex));
                sb.AppendLine("异常信息: " + ex.Message);
                sb.AppendLine("StackTrace: " + ex.StackTrace);
                Trace.Write(sb.ToString());
            }

            return table;
        }
예제 #3
0
파일: Main.cs 프로젝트: gavincode/ExcelTool
        /// <summary>
        /// 为每个TabPage附加数据
        /// </summary>
        /// <param name="excel">Excel对象</param>
        private void AppendDataToTabPages(MoqikakaExcel excel)
        {
            DataTable table = null;
            String sheetName;

            //循环读取每个Excel表单的数据
            for (Int32 i = 0; i < excel.NumberOfSheets; i++)
            {
                sheetName = excel.GetSheetName(i);

                table = GlobalCacheBLL.GetSheetTable(sheetName, excel);

                //通知主线程控件,数据已准备好
                tabControlSheetInfo.Invoke(new MethodInvoker(() =>
                {
                    if (tabControlSheetInfo.TabPages.Count <= i) return;

                    tabControlSheetInfo.TabPages[i].Controls.Add(CreatSingleGridview(table));

                    //展示第一个表单信息
                    if (i == 0) ShowSheetInfo(sheetName, table);
                }));
            }
        }