在项目中使用到遇到了需要将excel文件导入到数据库中,在此做个总结记录,防止后面人踩坑。

    开发环境:VS2008+Win10

第一种方式:Office.Interop.Excel方式  数据量大时有点慢

        public static DataTable getExcelDataKHFY(string fileName, DataTable dt)
        {
            string[] names = GetExcelTableName(fileName);
            int ysCount = 0;//获取页数
            string exname = "";
            if (names.Length > 1)
            {
                Yx_Module.winChangeExcel win = new THKClient.Yx_Module.winChangeExcel(names);

                win.ShowDialog();
                if (win.IsReturn)
                {
                    ysCount = int.Parse(win.Excelid) + 1;//页码
                    exname = win.ExName;//选择的页名
                }

            }

            DataTable result = dt;
            Microsoft.Office.Interop.Excel.Application xlApp = null;
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook = null;
            try
            {
                xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null) return null;

                xlApp.Visible = false;
                xlWorkbook = xlApp.Workbooks.Open(fileName, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
                Dictionary<int, Microsoft.Office.Interop.Excel.Range> rowrange = new Dictionary<int, Microsoft.Office.Interop.Excel.Range>();
                if (ysCount != 0)
                {
                    try
                    {
                        //动态页插入数据
                        int i = ysCount;
                        // Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Sheets[i];//根据页码
                        Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Sheets[exname];//根据页名获取
                        string str = sheet.Name;
                        for (int rowIndex = 2; rowIndex < sheet.Cells.Rows.Count - 1; rowIndex++)
                        {
                            rowrange.Clear();
                            object[] parm = new object[result.Columns.Count];
                            for (int j = 0; j < result.Columns.Count; j++)
                            {
                                rowrange[j] = (Microsoft.Office.Interop.Excel.Range)(sheet.Cells[rowIndex, j + 1]);

                                parm[j] = rowrange[j].Value2 == null ? "" : rowrange[j].Value2.ToString();
                            }



                            rowrange.Clear();
                            if (parm[0].ToString().Trim().Length == 0)
                            {
                                break;
                            }
                            result.Rows.Add(parm);
                        }
                    }
                    catch (Exception ee)
                    {

                        throw;
                    }
                }
                else
                {
                    for (int i = 1; i < xlWorkbook.Sheets.Count + 1; i++)
                    {
                        Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Sheets[i];
                        string str = sheet.Name;
                        for (int rowIndex = 2; rowIndex < sheet.Cells.Rows.Count - 1; rowIndex++)
                        {
                            rowrange.Clear();
                            object[] parm = new object[result.Columns.Count];
                            for (int j = 0; j < result.Columns.Count; j++)
                            {
                                rowrange[j] = (Microsoft.Office.Interop.Excel.Range)(sheet.Cells[rowIndex, j + 1]);

                                parm[j] = rowrange[j].Value2 == null ? "" : rowrange[j].Value2.ToString();
                            }



                            rowrange.Clear();
                            if (parm[0].ToString().Trim().Length == 0)
                            {
                                break;
                            }
                            result.Rows.Add(parm);
                        }
                    }
                }

                xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
                xlApp.Quit();
            }
            finally
            {
                Marshal.ReleaseComObject(xlWorkbook);
                Marshal.ReleaseComObject(xlApp);
                xlApp = null;
                List<Process> excelProcesses = GetExcelProcesses();
                if (excelProcesses.Count > 0)
                {
                    KillTheExcel();//杀死进程

                }
            }

            return result;
        }
View Code

相关文章: