Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将excel区域复制到未格式化的文本(.txt)文件中,使所有单元格形成一个字符串,而不是单独的项?C#_C#_Excel_Visual Studio_Text_Notepad - Fatal编程技术网

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

如何将excel区域复制到未格式化的文本(.txt)文件中,使所有单元格形成一个字符串,而不是单独的项?C#,c#,excel,visual-studio,text,notepad,C#,Excel,Visual Studio,Text,Notepad,我在excel工作表的B列中获得所有“好”单元格的范围,然后在“D”列中找到相应的单元格并创建这些单元格的范围。我想将所有这些单元格转换为一个字符串,并将其粘贴到我的记事本文件中,这样每个单元格的字符串之间就没有空格,它们显示在一行上 现在,我的代码将每个单元格项读取为自己的实体,并将它们打印在单独的行上。我希望能够迭代一个字符串,所以我希望它们都形成一个完整的字符串 Microsoft.Office.Interop.Excel.Application excelApp = new Micros

我在excel工作表的B列中获得所有“好”单元格的范围,然后在“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);

在写入文件之前,请尝试在剪贴板文本中使用此选项-它会删除每个单元格项中的空白,但不会将所有单元格项连接在一起形成一个字符串。它们仍然在单独的行上。您在这里想要实现的是什么?看起来您正在遍历B列中的单元格列表-对于包含字符串“Good”的每个单元格-您重新创建单元格地址,然后提取内容(您已经知道是“Good”)&将其复制到剪贴板,然后将其写入文件(而不是附加)。如果代码按照您解释的那样工作,那么您可能会获得很好的效果。我对其进行了编辑,使其更简单,但基本上在我的原始代码中,我将好的单元格地址转换为另一个地址(也就是说,原始地址是B1,我将其转换为D1和大量与B地址相对应的D单元格,我将更改脚本以反映这一点,以避免混淆。基本上,我想要一个包含所有D单元格内容的长字符串。与您在9月7日提出的问题相同:最终使用您的答案解决了,谢谢!)!!!:D“RequiredOutputString”成功了。