ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.How to set height, width, background color, font color of excel using Microsoft.Office.Interop.Excel library in C#<p>Hi <a class="username" rel="santosh86"> santosh86</a>,</p> <p>Will you please share the code which you are using.</p>https://www.aspforums.net:443/Threads/946813/How-to-set-height-width-background-color-font-color-of-excel-using-MicrosoftOfficeInteropExcel-library-in-C/https://www.aspforums.net:443/Threads/946813/How-to-set-height-width-background-color-font-color-of-excel-using-MicrosoftOfficeInteropExcel-library-in-C/Sat, 22 Oct 2016 02:30:37 GMTHow to set height, width, background color, font color of excel using Microsoft.Office.Interop.Excel library in C#<p><br /> DataTable dt = SqlHelper.ReturnDataTable("select companymasterid ,name as 'Company Name' ,ISNULL(TotalToken, 0 ) as 'Total Tokens',ISNULL(LeftToken, 0 ) as 'Approve Tokens',(ISNULL(TotalToken, 0 ) - ISNULL(LeftToken, 0 )) as 'Remaining Tokens' from companymaster where isactive='Y' and companymasterid=" + companyId + " and companymasterid !=0 order by name asc");<br /> if (dt.Rows.Count &gt; 0)<br /> {<br /> string path = Server.MapPath("exportedfiles\\");</p> <p>if (!Directory.Exists(path)) // CHECK IF THE FOLDER EXISTS. IF NOT, CREATE A NEW FOLDER.<br /> {<br /> Directory.CreateDirectory(path);<br /> }</p> <p>File.Delete(path + "EmployeeDetails.xlsx"); // DELETE THE FILE BEFORE CREATING A NEW ONE.</p> <p><br /> // ADD A WORKBOOK USING THE EXCEL APPLICATION.<br /> Excel.Application xlAppToExport = new Excel.Application();<br /> xlAppToExport.Workbooks.Add("");</p> <p>// ADD A WORKSHEET.<br /> Excel.Worksheet xlWorkSheetToExport = default(Excel.Worksheet);<br /> xlWorkSheetToExport = (Excel.Worksheet)xlAppToExport.Sheets["Sheet1"];<br /> // ROW ID FROM WHERE THE DATA STARTS SHOWING.<br /> int iRowCnt = 2;<br /> Excel.Range range = xlWorkSheetToExport.Cells[2, 1] as Excel.Range;<br /> range.EntireRow.Font.Name = "Calibri";<br /> // xlWorkSheetToExport.Cells[2, 1].Font.Bold = true;<br /> //range.Rows.AutoFit();<br /> //range.Columns.AutoFit();<br /> //Excel.Range range;<br /> <br /> //range.EntireRow.Font.Bold = true;<br /> //range.EntireRow.Font.Size = 20;<br /> Microsoft.Office.Interop.Excel.Range oRange = (Microsoft.Office.Interop.Excel.Range)xlWorkSheetToExport.Cells[2, 3];<br /> //oRange.ColumnWidth = 40;<br /> //oRange.RowHeight = 45;<br /> oRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;<br /> float Left = (float)((double)oRange.Left);<br /> float Top = (float)((double)oRange.Top);<br /> //ws.Cells["A1:K20"].AutoFitColumns();<br /> //oRange("E:E").ColumnWidth = 17.29;<br /> //const float ImageSize = 32;<br /> //add image in particular cells with Karco logo<br /> //xlWorkSheetToExport.Shapes.AddPicture("C:\\Users\\admin\\Desktop\\images.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 165, 20, ImageSize, ImageSize);<br /> //xlWorkSheetToExport.Range["A1:E1"] = true; // MERGE CELLS OF THE HEADER. <br /> //xlWorkSheetToExport.Cells[3, 3] = "Karishma Marine Solutions Pvt Ltd.";<br /> <br /> //xlWorkSheetToExport.Cells[4, 3] = " KARCO CBT VER 1.2";<br /> // SHOW COLUMNS ON THE TOP.<br /> xlWorkSheetToExport.Cells[iRowCnt - 1, 1] = "Company Name";<br /> xlWorkSheetToExport.Cells[iRowCnt - 1, 2] = "Total Tokens";<br /> xlWorkSheetToExport.Cells[iRowCnt - 1, 3] = "Approve Tokens";<br /> xlWorkSheetToExport.Cells[iRowCnt - 1, 4] = "Remaining Tokens";<br /> //xlWorkSheetToExport.Cells[iRowCnt - 1, 5] = "timestamp";<br /> int i;<br /> for (i = 0; i &lt;= dt.Rows.Count - 1; i++)<br /> {<br /> xlWorkSheetToExport.Cells[iRowCnt, 1] = dt.Rows[i].Field&lt;string&gt;("Company Name");<br /> xlWorkSheetToExport.Cells[iRowCnt, 2] = dt.Rows[i].Field&lt;int&gt;("Total Tokens");<br /> xlWorkSheetToExport.Cells[iRowCnt, 3] = dt.Rows[i].Field&lt;int&gt;("Approve Tokens");<br /> xlWorkSheetToExport.Cells[iRowCnt, 4] = dt.Rows[i].Field&lt;int&gt;("Remaining Tokens");<br /> <br /> //range.EntireColumn.ColumnWidth = 50.43;<br /> //xlWorkSheetToExport.Cells[iRowCnt, 5] = dt.Rows[i].Field&lt;int&gt;("timestamp");</p> <p>iRowCnt = iRowCnt + 1;<br /> }<br /> // FINALLY, FORMAT THE EXCEL SHEET USING EXCEL'S AUTOFORMAT FUNCTION.<br /> Excel.Range range11 = xlAppToExport.ActiveCell.Worksheet.Cells[4, 1] as Excel.Range;<br /> //range11.AutoFormat(ExcelAutoFormat.xlRangeAutoFormatList3);</p> <p>// SAVE THE FILE IN A FOLDER.<br /> xlWorkSheetToExport.SaveAs(path + "EmployeeDetails.xlsx");<br /> // CLEAR.<br /> xlAppToExport.Workbooks.Close();<br /> xlAppToExport.Quit();<br /> xlAppToExport = null;<br /> xlWorkSheetToExport = null;<br /> //string path = Server.MapPath("exportedfiles\\");</p> <p>// CHECK IF THE FOLDER EXISTS.<br /> if (Directory.Exists(path))<br /> {<br /> // CHECK IF THE FILE EXISTS.<br /> if (File.Exists(path + "EmployeeDetails.xlsx"))<br /> {<br /> string sPath = Server.MapPath("exportedfiles\\");</p> <p>Response.AppendHeader("Content-Disposition", "attachment; filename=EmployeeDetails.xlsx");<br /> Response.TransmitFile(sPath + "EmployeeDetails.xlsx");<br /> Response.End();</p> <p>}<br /> }<br /> }<br /> }<br /> catch (Exception ex)<br /> {<br /> //lblConfirm.Text = ex.Message.ToString();<br /> //lblConfirm.Attributes.Add("style", "color:red; font: bold 14px/16px Sans-Serif,Arial");<br /> }<br /> finally<br /> {<br /> //sda.Dispose();<br /> //sda = null;<br /> }</p>https://www.aspforums.net:443/Threads/946813/How-to-set-height-width-background-color-font-color-of-excel-using-MicrosoftOfficeInteropExcel-library-in-C/https://www.aspforums.net:443/Threads/946813/How-to-set-height-width-background-color-font-color-of-excel-using-MicrosoftOfficeInteropExcel-library-in-C/Sat, 22 Oct 2016 02:32:02 GMTHow to set height, width, background color, font color of excel using Microsoft.Office.Interop.Excel library in C#<p>Hi <a class="username" rel="santosh86">santosh86</a>,</p> <p>Refer the below code.</p> <pre class="brush: csharp">// Assigning value to cell. xlWorkSheetToExport.Cells[3, 3] = &#34;Karishma Marine Solutions Pvt Ltd.&#34;; // Get the range from excel. Excel.Range range = xlAppToExport.ActiveCell.Worksheet.Cells[3, 3] as Excel.Range; // Auto resize cell width and height. //range.Columns.AutoFit(); // Set background color. range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); // Set font color. range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White); // Set Row Height. range.EntireRow.RowHeight = 50.0; // Set Column Width. range.ColumnWidth = 50.0;</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/946813/How-to-set-height-width-background-color-font-color-of-excel-using-MicrosoftOfficeInteropExcel-library-in-C/https://www.aspforums.net:443/Threads/946813/How-to-set-height-width-background-color-font-color-of-excel-using-MicrosoftOfficeInteropExcel-library-in-C/Sat, 22 Oct 2016 03:05:05 GMTHow to set height, width, background color, font color of excel using Microsoft.Office.Interop.Excel library in C#<p>thanks sir</p>https://www.aspforums.net:443/Threads/946813/How-to-set-height-width-background-color-font-color-of-excel-using-MicrosoftOfficeInteropExcel-library-in-C/https://www.aspforums.net:443/Threads/946813/How-to-set-height-width-background-color-font-color-of-excel-using-MicrosoftOfficeInteropExcel-library-in-C/Sat, 22 Oct 2016 03:45:53 GMT