如何将 excel 范围复制到未格式化的文本 (.txt) 文件,以便所有单元格形成一个字符串而不是单独的项目? C#

How do I copy an excel range to Text (.txt) file, unformatted so that all cells form one single string and are not separate items? C#

我在 excel sheet 的 B 列中获取所有 "Good" 单元格的范围,然后在 "D" 列中找到相应的单元格并创建一个范围那些细胞。我想将所有这些单元格转换为一个字符串并将其粘贴到我的记事本文件中,以便每个单元格的字符串之间没有空格并且它们显示在一行中。

现在我的代码将每个单元格项目作为其自己的实体读取,并将它们打印在不同的行上。我希望能够遍历一个字符串,所以我希望它们都形成一个完整的字符串。

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(comboBox2.Text);
            Excel.Worksheet xlWorkSheet = 

(Excel.Worksheet)excelWorkbook.Sheets[sheetSpaces];
            excelApp.Visible = false;
            excelApp.ScreenUpdating = false;
            excelApp.DisplayAlerts = false;
            Excel.Range last = xlWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            int lastUsedRow = last.Row;
            string lastCell = "B" + lastUsedRow.ToString();
            Excel.Range range = xlWorkSheet.get_Range("B1", lastCell);


            foreach (Excel.Range item in range.Cells)
            {

                string text = (string)item.Text;
                if (text == "Good")
                {
                    //get address of all Good items 
                    string textx = (string)item.Address;

   //change address of Good items to corresponing address in D column
                string textxcorrect = textx.Replace("B", "D");

                //get rid of "$" for address
                var cellAddress = textxcorrect.Replace("$", "");

                //create range for addresses with the new D column addresses
                Excel.Range xlRng = xlWorkSheet.get_Range(cellAddress, Type.Missing);
                    string fileLocation = @"C:\Users\npinto\Desktop\hopethisworks.txt";


               foreach (Excel.Range item2 in xlRng)
                    {
                        xlRng.Copy();

                        File.WriteAllText(fileLocation, Clipboard.GetText());

                    }




                    string readText = System.IO.File.ReadAllText(fileLocation);
                    Console.WriteLine(readText);

我已经根据您原来的问题更新了我的答案 - 如果我现在理解正确的话,B 行中的单元格将包含单词 "Good" - D 列中同一行中的单元格将包含一个单元格参考 - 例如 A4 & 你想附加该数据。

注意 - 如果 D 列单元格包含“+A4” - 则返回的文本将是您需要附加的文本 - 因此只需连接 nextAddress 而不是获取 xlRng2。

这个怎么样 - 根据文本的大小,您可能希望使用 StringBuilder 而不是字符串 - 但是对于少量数据,不会有任何显着差异。

string RequiredOutputString = String.Empty;
foreach (Excel.Range item in range.Cells)
{
  string text = (string)item.Text;
  if (text == "Good")
  {
    //get address of all Good items 
    string textx = (string)item.Address;

    //change address of Good items to corresponing address in D column
    var cellAddress = textx.Replace("$B", "D");
    // get a reference to cell in column D
    Range xlRng = curWorkSheet.get_Range(cellAddress, Type.Missing);
    // get the cell address in row D cell
    string nextAddr = xlRng.Text;
    // get a reference to the cell point to from Row D
    Range xlRng2 = curWorkSheet.get_Range(nextAddr, Type.Missing);
    // append that cell contents
    RequiredOutputString += xlRng2.Text.Trim();
  }
}

string fileLocation = @"C:\Users\npinto\Desktop\hopethisworks.txt";
File.WriteAllText(fileLocation, RequiredOutputString);