简体   繁体   中英

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#

I get a range of all "Good" cells in Column B of my excel sheet, the find the corresponding cells in the "D" Column and create a range of those cells. I want to convert all those cells to one single string and paste that to my notepad file, so that there are no spaces between each cell's strings and they are displayed on a single line.

Right now my code reads each cell item as its own entity and prints them on separate lines. I want to be able to iterate over one single string, so I would like them to all form one whole string.

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);

I have updated my answer based on your original question - if I now understand correctly the cell in row B will contain the word "Good" - the cell in the same row in column D will contain a single Cell reference - eg A4 & you want to append that data.

NOTE - if the column D cell contains "+A4" - then the text returned will be what you require to be appended - so just concatenate nextAddress rather than get xlRng2.

How about this - depending on the size of the text you may want to use a StringBuilder rather than string - but with small amounts of data there wont be any significant difference.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM