• C#操作Excel的几个函数


    //获得某个文本的在excel的sheet中的位置

    public Position GetPosition(Worksheet mySheet,string strText)
            {
                Position positon = new Position();
                try
                {
                    Range currentFind = mySheet.Cells.Find(strText, Type.Missing,
                                Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
                                Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
                                Type.Missing, Type.Missing);
                    if (currentFind!=null)
                    {
                        positon.Row = currentFind.Row;
                        positon.Column = currentFind.Column;
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error:" + e.Message + " please contact the administrator!");
                }
                return positon;
            }

     //获得位置,给定区域内获取

            public Position GetPosition(Worksheet mySheet, string strText,int MaxRow,int MinRow,int Column,decimal Amount)
            {
                Position positon = new Position();
                bool bFind = false;
                bool bComplete = false;
                try
                {
                    if (strText=="")
                    {
                        return positon;
                    }
                    Range currentFind = mySheet.Cells.Find(strText, Type.Missing,
                                Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
                                Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
                                Type.Missing, Type.Missing);

                    if (currentFind != null)
                    {
                        if ((currentFind.Row >= (MinRow + 1)) && (currentFind.Row <= MaxRow) && (currentFind.Column == (Column + 1)))
                        {
                            Range range = (Range)mySheet.Cells[currentFind.Row, currentFind.Column + 2];
                            string strResult = "";
                            if (range.Value2 != null)
                            {
                                strResult = range.Value2.ToString();
                            }
                            decimal currentAmount = decimal.Parse(strResult);
                            if (currentAmount == Amount)
                            {
                                bFind = true;
                                bComplete = true;
                                positon.Row = currentFind.Row;
                                positon.Column = currentFind.Column;
                            }
                        }
                    }
                    else
                    {
                        bComplete = true;
                    }
                    //continue to find
                    Range rangeFind = currentFind;
                     while (!bComplete)
                     {
                         if (!bFind)
                         {
                             Range nextFind = mySheet.Cells.FindNext(rangeFind);
                             if (nextFind != null)
                             {
                                 if (!((nextFind.Row == currentFind.Row) && (nextFind.Column == currentFind.Column)))
                                 {
                                     rangeFind = nextFind;
                                     if ((nextFind.Row >= (MinRow + 1)) && (nextFind.Row < MaxRow) && (nextFind.Column == (Column + 1)))
                                     {
                                         Range range = (Range)mySheet.Cells[nextFind.Row, nextFind.Column + 2];
                                         string strResult = "";
                                         if (range.Value2 != null)
                                         {
                                             strResult = range.Value2.ToString();
                                         }
                                         decimal currentAmount = decimal.Parse(strResult);
                                         if (currentAmount == Amount)
                                         {
                                             bFind = true;
                                             bComplete = true;
                                             positon.Row = nextFind.Row;
                                             positon.Column = nextFind.Column;
                                         }
                                     }
                                 }
                                 else
                                 {
                                     bComplete = true;
                                 }
                              
                             }
                             else
                             {
                                 bComplete = true;
                             }
                           
                         }
                     }
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error:" + e.Message + " please contact the administrator!");
                }
                return positon;
            }

     //插入行

                    if (iRowNeedInsert > 0)
                    {
                        //insert blank rows
                        Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)mySheet1.Rows[pSDTotal1.Row - 1, missing];
                        for (int i = 0; i < iRowNeedInsert; i++)
                        {
                            range.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, missing);
                        }
                    }
     //复制和粘贴

                    rCopy1 = "G" + (pSD2.Row + 1).ToString() + " ";
                    rCopy2 = "J" + (pSDLast2.Row).ToString() + " ";
                    rPaste = "G" + (pSDLast1.Row + 1).ToString() + " ";
                    mySheet2.get_Range(rCopy1, rCopy2).Copy(System.Type.Missing);
                    mySheet1.get_Range(rPaste, System.Type.Missing).PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteAll, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, System.Type.Missing, System.Type.Missing);

     //置为空

     mySheet1.get_Range(mySheet1.Cells[iCountLC, pLC1.Column], mySheet1.Cells[iCountLC, pLC1.Column + 4]).Value2 = "";

     //排序

       Microsoft.Office.Interop.Excel.Range rngLC = mySheet1.get_Range(mySheet1.Cells[pLC1.Row + 1, pLC1.Column], mySheet1.Cells[pLCLast1.Row, pLC1.Column + 4]);
                    rngLC.Sort(rngLC, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                        missing, missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                        missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                        Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
                                        Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
                                        Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
                                        Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                                        Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                                        Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);

  • 相关阅读:
    Java中的线程Thread方法之---interrupt() 分类: Android Java 2014-02-26 08:51 3189人阅读 评论(2) 收藏
    Java中的对象Object方法之---wait()和notifiy() 分类: Java Android 2014-02-26 08:50 1599人阅读 评论(0) 收藏
    Java中的线程Thread方法之---suspend()和resume() 分类: Java 2014-02-25 14:37 1650人阅读 评论(0) 收藏
    Java中的线程Thread方法之---join() 分类: Android Java 2014-02-25 13:38 1393人阅读 评论(0) 收藏
    Java中的线程Thread方法之---stop() 分类: Java 2014-02-25 09:59 3075人阅读 评论(1) 收藏
    抓包工具Fidder详解(主要来抓取Android中app的请求) 分类: Android 2014-02-24 09:32 10064人阅读 评论(5) 收藏
    XML的解析 分类: JavaWeb Java Android 2014-02-17 18:22 1764人阅读 评论(3) 收藏
    XML文件定义约束 分类: JavaWeb 2014-02-17 17:49 1127人阅读 评论(0) 收藏
    Android中的广播Broadcast详解 分类: Android 2014-02-13 10:59 8414人阅读 评论(5) 收藏
    GitHub错误处理:fatal:could not read Username for 'https://github.com': No such file or directory 分类: Java 2014-02-11 19:39 2346人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/catvi/p/1952946.html
Copyright © 2020-2023  润新知