C#NPOI操作EXCEL时进行复制单元格或复制区域的相关操作

以下为进行研发EXCEL操作时进行的相关测试代码,记录如下。

 private void test(string filePath)
        {

           try
            {


                Microsoft.Office.Interop.Excel.Application ThisApplication = new Microsoft.Office.Interop.Excel.Application();
                var ThisWorkbook = ThisApplication.Workbooks.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                ThisApplication.DisplayAlerts = false;
                var xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)ThisWorkbook.Worksheets["sheet1"];

                Microsoft.Office.Interop.Excel.Range range = xlSheet.get_Range("P1", Type.Missing);                
              
                Microsoft.Office.Interop.Excel.Range range1 = xlSheet.get_Range("C6", Type.Missing);
                range.Copy(range1);

                ThisWorkbook.Save();//.SaveAs(filePath, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                ThisWorkbook.Close();

                if (ThisWorkbook != null)
                {
                    ThisWorkbook.Close(true, Type.Missing, Type.Missing);
                    ThisWorkbook = null;
                }
                if (ThisApplication != null)
                {
                    ThisApplication.Quit();
                    KillSpecialExcel(ThisApplication);
                    ThisApplication = null;
                }


            }
            catch(Exception ex)
            {


            }
            finally
            {
              
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            //IWorkbook workbook = null;// new HSSFWorkbook();// 创建一个Excel文件    
            //    ISheet sheet = null;// workbook.CreateSheet() as HSSFSheet;// 创建一个Excel的Sheet    
            //    //var a = new HSSFRichTextString("þ主要负责人");
            //    IRichTextString a = null;
            //    FileStream fs = null;
            //    ICell cell = null;


            //    using (fs = System.IO.File.OpenRead(filePath))
            //    {
            //        // 2007版本  
            //        if (filePath.IndexOf(".xlsx") > 0)
            //        {
            //            workbook = new XSSFWorkbook(fs);
            //            a = new XSSFRichTextString("þ主要负责人");
            //        }
            //        // 2003版本  
            //        else if (filePath.IndexOf(".xls") > 0)
            //        {
            //            workbook = new HSSFWorkbook(fs);
            //            a = new HSSFRichTextString("þ主要负责人");
            //        }

            //            if (workbook != null)
            //        {
            //            sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet  

            //            if (sheet != null)
            //            {
            //                var font1 = (HSSFFont)sheet.Workbook.CreateFont();
            //                font1.FontName = "Wingdings 2";
            //                font1.FontHeightInPoints = 14;
            //                a.ApplyFont(0, 1, font1);

            //                cell = sheet.GetRow(1).GetCell(16);
            //                Microsoft.Office.Interop.Excel.Range rang = sheet.get_Range("A1", "F7");

            //                sheet.GetRow(5).GetCell(3).SetCellValue(a);
            //                sheet.GetRow(5).GetCell(3).SetCellValue(a);

            //                sheet.GetRow(6).GetCell(3).SetCellValue(cell.RichStringCellValue);
            //                sheet.GetRow(7).GetCell(3).SetCellValue(cell.RichStringCellValue);

            //                sheet.GetRow(8).GetCell(3).SetCellValue(cell.RichStringCellValue);

            //                sheet.GetRow(9).GetCell(3).SetCellValue(cell.RichStringCellValue);
            //            }
            //        }
            //    }

            //    using (fs = System.IO.File.OpenWrite(filePath))
            //    {
            //        workbook.Write(fs);//向打开的这个xls文件中写入数据  

            //    }

        }
        #region DllImport Methods
        [System.Runtime.InteropServices.DllImport("User32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
        #endregion
        public static void KillSpecialExcel(Microsoft.Office.Interop.Excel.Application m_objExcel)
        {
            try
            {
                if (m_objExcel != null)
                {
                    int lpdwProcessId;
                    GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId);
                    System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
                }
            }
            catch (Exception)
            { }
        }
        public ActionResult Approve(string keyvalue)
        {
            var res = app.Approve(keyvalue);
            if (res)
            {
                return Success("批准成功");
            }
            return Error("批准失败!请确认状态是否正确");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Andrewniu/article/details/89677123