如何在Excel工作表中搜索特定字符串 [英] How search particular string in excel sheet

查看:144
本文介绍了如何在Excel工作表中搜索特定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用以下方法将数据写入excel表。



我需要在excel表的特定列中找到特定的字符串。



如果我找到那个特定的字符串。然后我需要将整行复制到不同的excel表格



可以建议一些方法吗?



Hi,

I am writing the data to excel sheet using the following method.

I need to find particular string in particular column of the excel sheet.

If I find that particular string. Then I need to copy the whole row to different excel sheet

Can any suggest some method about this?

private void PrintExcelSheet(CANBusDetails busDetails)
        {
            if (busDetails == null) return;

            object misValue = System.Reflection.Missing.Value;
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlApp.StandardFont = Constants.FONT_NAME;
            xlApp.StandardFontSize = Constants.FONT_SIZE;

            try
            {
                string path = Properties.Settings.Default.OutputDirPath + "\\" + busDetails.OutputExcelFileName;

                // Update Progressbar
                UpdateProgressBar(Constants.PERCENT_PRINTING_EXCEL_STARTED, Constants.PRINTING_EXCEL);

                if (File.Exists(path))
                    File.Delete(path);
                // Excel Sheetname and column names
                xlWorkSheet.Name = busDetails.OutputExcelFileName.Replace(Constants.XLS, string.Empty);

                xlWorkSheet.Range["A1"].Value2 = Constants.MSG_NAME;
                xlWorkSheet.Range["A1"].Font.Bold = true;
                xlWorkSheet.Range["B1"].Value2 = Constants.MSG_ID;
                xlWorkSheet.Range["B1"].Font.Bold = true;
                xlWorkSheet.Range["C1"].Value2 = Constants.TX_NODE;
                xlWorkSheet.Range["C1"].Font.Bold = true;
                xlWorkSheet.Range["D1"].Value2 = Constants.PRIORITY;
                xlWorkSheet.Range["D1"].Font.Bold = true;
                xlWorkSheet.Range["E1"].Value2 = Constants.CYCLE_TIME;
                xlWorkSheet.Range["E1"].Font.Bold = true;
                xlWorkSheet.Range["F1"].Value2 = Constants.CYCLE_TYPE;
                xlWorkSheet.Range["F1"].Font.Bold = true;
                int row = 2;

                foreach (CANMessageDetails temp in busDetails.CANMessageDetailsList)
                {
                    xlWorkSheet.Cells[row, Constants.OP_EXCEL_WR_CAN_MESSAGE_NAME_INDEX] = temp.MessageName;
                    xlWorkSheet.Cells[row, Constants.OP_EXCEL_WR_HEX_MSGID_INDEX] = temp.HexMessageId;
                    xlWorkSheet.Cells[row, Constants.OP_EXCEL_WR_TX_NODE_NAME_INDEX] = temp.TxNode;
                    xlWorkSheet.Cells[row, Constants.OP_EXCEL_WR_CAN_MESSAGE_PRIORITY_INDEX] = temp.Priority;
                    xlWorkSheet.Cells[row, Constants.OP_EXCEL_WR_CAN_MESSAGE_CYCLETIME_INDEX] = temp.CycleTime;
                    xlWorkSheet.Cells[row, Constants.OP_EXCEL_WR_CAN_MESSAGE_CYCLETYPE_INDEX] = temp.CycleType;
                    row++;
                }

                xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
            }
            catch (Exception ex)
            {
                throw new Exception("Could not load Message details to excel\nDetails: " + ex.Message);
            }
            finally
            {
                ReleaseObject(xlWorkSheet);
                ReleaseObject(xlWorkBook);
                ReleaseObject(xlApp);
            }
         
        }





谢谢

John



Thanks
John

推荐答案

以下是用于在c#中的Excel工作表中搜索特定文本的示例代码。这里是Find()方法,它查找范围中的特定文本,并返回一个Range对象,该对象表示找到该文本的第一个单元格。找到文本后,您可以使用C#中的某些内容轻松复制整行。



http://www.authorcode.com/search-text-in-excel-file-through-c/ [ ^ ]
Here is a sample code used to search particular text in an Excel Sheet in c#. Here the Find() method that Finds specific text in a range and returns a Range object that represents the first cell where that text is found. Once the text is found then you can easily copy the entire row using some stuff in C#.

http://www.authorcode.com/search-text-in-excel-file-through-c/[^]


这篇关于如何在Excel工作表中搜索特定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆