C#_无插件读取Excel_拷贝表并访问

using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public  class SaveResult
    {
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlsWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet newWorksheet,Base;

        bool IsSheetExist = false;
       
        public void SaveResultReport(int ProductType,int ProductNum)//保存报表
        {
            //MessageBox.Show(System.AppDomain.CurrentDomain.BaseDirectory);//基目录
            xlApp.DisplayAlerts = false;
            xlApp.Visible = false;
            xlApp.ScreenUpdating = false;
            switch (ProductType)
            {
                case 0://A
                    switch (ProductNum)
                    {
                        case 0://增速
                            xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\A增速.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
            System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
                            break;
                        case 1://续航
                            xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\A续航.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
            System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
                            break;
                        case 2://壳体
                            xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\A壳体.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
             System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
                            break;
                    }
                    break;
                case 1://B
                    switch (ProductNum)
                    {
                        case 0://增速
                            xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\B增速.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
             System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
                            break;
                        case 1://续航
                            xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\B续航.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
            System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
                            break;
                        case 2://壳体
                            xlsWorkBook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\B壳体.xlsx", System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
            System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//打开excel
                            break;
                    }
                    break;
            }
            foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in xlsWorkBook.Worksheets)
            {
                if (sheet.Name == DateTime.Now.ToString("yyyyMMdd"))
                    IsSheetExist = true;
                else if (sheet.Name =="Base")//基础表
                {
                    Base = sheet;
                }
            }
            if (!IsSheetExist)
            {
                //newWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlsWorkBook.Worksheets.Add(System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);//添加新表
                // newWorksheet.Name = DateTime.Now.ToString("yyyyMMdd");
                Base.Copy(xlsWorkBook.Worksheets[1]);
                newWorksheet = xlsWorkBook.Worksheets[1];
                newWorksheet.Name = DateTime.Now.ToString("yyyyMMdd");
            }
            else
            {
                newWorksheet = xlsWorkBook.Worksheets[1];
                MessageBox.Show("已存在表单");
            }
            xlsWorkBook.Save();
            try
            {
                xlsWorkBook.Close(null, null, null);
                xlApp.Quit();
                System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcesses();
                foreach (System.Diagnostics.Process p in process)
                {
                    if (p.ProcessName.ToLower() == "EXCEL.exe")
                    {
                        p.Kill();
                    }
                }
                GC.Collect();
            }
            catch (Exception)
            {
                //正常退出Excel失败,可以记录一下日志.
                MessageBox.Show("退出Excel失败");

            }
        }

    }
发布了11 篇原创文章 · 获赞 1 · 访问量 154

猜你喜欢

转载自blog.csdn.net/qq_38069903/article/details/105139543