簡體   English   中英

如何刪除Excel中的某一行?

[英]How to delete a certain row in Excel?

我看了很多關於如何刪除Excel中某一行的教程。請幫助mo使用c#刪除excel中的一行。fileReader,FileWriter和Splitter已經可以工作了。 我現在唯一的問題是如何刪除 Excel 中的某一行。

Class 變量

     public static string fileName = @".\Contestant.csv";
     public static string[,] contestant;

主要方法

   List<string> lines = fileReader(fileName);
        while (i < lines.Count)
        {
            string[] temp = stringSplitter(lines[i], new char[] { ',' });
            // a contains how many elements in the array
            a = temp.Count();
            // divides a and plus by 1 to know how many arrays there should be in the 2d array
            d = (a / 2) + 1;
            contestant = new string[a, d]; 

這是我的 FileReader 代碼

  static List<string> fileReader(string filePath)
    {
        List<string> lines = new List<string>();
        try
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                string line = "";

                while ((line = sr.ReadLine()) != null)
                {
                    lines.Add(line);
                }
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("Error Message: Please close the file and try again");
            //Console.WriteLine(e); for more detailed errors
        }
        return lines;
    }

這是我的 FileWriter 代碼

   static void fileWriter(string filePath, bool appendFlag, string message)
    {
        using (StreamWriter sr = new StreamWriter(filePath, appendFlag))
        {
            sr.WriteLine(message);
        }
    }

這是用於拆分器字符串

    static string[] stringSplitter(string stringToSplit, char[] splitChars)
    {
        return stringToSplit.Split(splitChars);
    }

我建議在列表中完全操縱您的日期,然后用新信息替換整個文檔。 所以閱讀所有 -> 操作 -> 用新內容替換你的文檔。

也不要忘記在讀/寫后關閉 FileStreams。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM