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