如何使用c#来清除Excel工作表的所有数据 [英] how to chek all data of excel sheet using c#

查看:1826
本文介绍了如何使用c#来清除Excel工作表的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我要检查excel表格中的所有数据,如果任何单元格包含任何数据(0金额除外)我想显示消息此单元格包含除0金额请核实请帮助我

Hi friends,
I want check all data present in excel sheet ,if any of cell contain any data (except 0 amount) I want display message "this cell contain except 0 amount please verify" like that please help me on that

推荐答案

我写了一个示例示例。希望这可以帮助。在此示例中,它仅检查B列中的数据。您可以对所需的任何列执行相同操作。

I have written a sample example. Hope this helps. In this example it is checking for data only in column B. You can do the same for whatever columns you require.
void CheckExcel() 
        {

            string filePath = @"F:\Book2.xlsx";

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;

            try
            {
                xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 0);

                Microsoft.Office.Interop.Excel.Worksheet worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlApp.Worksheets["Sheet1"];

                int sheet1LastRowCount = worksheet1.UsedRange.Rows.Count;

                for (int i = 2; i <=sheet1LastRowCount; i++)
                {
                        if (Convert.ToInt32(worksheet1.Range["B" + i, "B" + i].Value2) == 0)
                        {
                            Console.WriteLine("Value is equal to 0 in row "+i);
                        }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                xlApp.Quit();
            }


        }


适用于所有列和所有行。如果正确,请接受答案



For all columns and all rows. Please accept the answer if it's correct

try
           {
               xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 0);

               Microsoft.Office.Interop.Excel.Worksheet worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlApp.Worksheets["Sheet1"];

               int sheet1LastRowCount = worksheet1.UsedRange.Rows.Count;
               int _val=0;

               for (int i = 2; i <=sheet1LastRowCount; i++)
               {
                   for(int j=1;j<=worksheet1.UsedRange.Columns.Count;j++)
                   {
                       if( Int32.TryParse(Convert.ToString(worksheet1.Cells[i,j].Value2),out _val))
                       {
                           if (Convert.ToInt32(_val) == 0)
                           {
                               Console.WriteLine("Value is equal to 0 in row " + i + " and column " + j);
                           }
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.Message);
           }
           finally
           {
               xlApp.Quit();
           }


这篇关于如何使用c#来清除Excel工作表的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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