c# excel format each row in rtf -
i working on application has convert valuables each row rtf document. excel file using has more 10.000 rows 5 columns. far can read file usingvalue2.tostring
method. 5 values each row, provide them header
firsttext= text of cel a1
secondtext= text of cel b1
thirdtext= text of cel c1
fourthtext= text of cel d1
fifttext= text of cel e1
and same cel a2, b2 etc etc. code far
//create com objects. excel.application xlapp = new excel.application(); excel.workbook xlworkbook = xlapp.workbooks.open(@"c:\aaa.xlsx"); excel._worksheet xlworksheet = xlworkbook.sheets[1]; excel.range xlrange = xlworksheet.usedrange; int rowcount = xlrange.rows.count; int colcount = xlrange.columns.count; //iterate on rows , columns , print console appears in file //excel not 0 based!! (int = 1; <= rowcount; i++) { (int j = 1; j <= colcount; j++) { //new line if (j == 1) console.writeline("\r\n"); //write value console if (xlrange.cells[i, j] != null && xlrange.cells[i, j].value2 != null) console.write(xlrange.cells[i, j].value2.tostring() + "\t", "\r\n"); } // console.readline(); testing purposes } //cleanup gc.collect(); gc.waitforpendingfinalizers(); // rule of thumb releasing com objects: // never use 2 dots, com objects must referenced , released individually // ex: [somthing].[something].[something] bad //release com objects kill excel process running in background marshal.releasecomobject(xlrange); marshal.releasecomobject(xlworksheet); //close , release xlworkbook.close(); marshal.releasecomobject(xlworkbook); //quit , release xlapp.quit(); marshal.releasecomobject(xlapp); console.readline();
any appreciated
i'm still not sure problem is?
are asking on how add headers?
//iterate on rows , columns , print console appears in file //excel not 0 based!! (int = 1; <= rowcount; i++) { (int j = 1; j <= colcount; j++) { //new line if (j == 1) console.writeline("\r\n"); //write value console if (xlrange.cells[i, j] != null && xlrange.cells[i, j].value2 != null) console.write("{0}= ", getheadertext(j)); console.write(xlrange.cells[i, j].value2.tostring() + "\t", "\r\n"); } // console.readline(); testing purposes }
get header text via helper method:
private string getheadertext(int colid) { switch (colid) { case 1: return "firsttext"; case 2: return "secondtext"; case 3: return "thirdtext"; case 4: return "fourthtext"; case 5: return "fithtext"; default: return "header not defined"; } }
Comments
Post a Comment