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 range of "good" cells in column b of excel sheet, find corresponding cells in "d" column , create range of cells. want convert cells 1 single string , paste notepad file, there no spaces between each cell's strings , displayed on single line.

right code reads each cell item own entity , prints them on separate lines. want able iterate on 1 single string, them form 1 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 items                      string textx = (string)item.address;     //change address of items corresponing address in d column                 string textxcorrect = textx.replace("b", "d");                  //get rid of "$" address                 var celladdress = textxcorrect.replace("$", "");                  //create range addresses 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 answer based on original question - if understand correctly cell in row b contain word "good" - cell in same row in column d contain single cell reference - e.g a4 & want append data.

note - if column d cell contains "+a4" - text returned require appended - concatenate nextaddress rather xlrng2.

how - depending on size of text may want use stringbuilder rather string - small amounts of data there wont significant difference.

string requiredoutputstring = string.empty; foreach (excel.range item in range.cells) {   string text = (string)item.text;   if (text == "good")   {     //get address of items      string textx = (string)item.address;      //change address of items corresponing address in d column     var celladdress = textx.replace("$b", "d");     // reference cell in column d     range xlrng = curworksheet.get_range(celladdress, type.missing);     // cell address in row d cell     string nextaddr = xlrng.text;     // reference cell point row d     range xlrng2 = curworksheet.get_range(nextaddr, type.missing);     // append cell contents     requiredoutputstring += xlrng2.text.trim();   } }  string filelocation = @"c:\\users\\npinto\\desktop\\hopethisworks.txt"; file.writealltext(filelocation, requiredoutputstring); 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -