473,511 Members | 16,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to set properties to excell sheet by c#?

nirmalsingh
218 New Member
hai all,
i want to set width, alignment for excell sheet columns programatically through c#,
how to do this?
my coding
Expand|Select|Wrap|Line Numbers
  1.  public  void exportToExcel(DataGridView myGrid, string strHiddenCols,string strHeading)
  2.         {
  3.             string[] strArrSplitCols;
  4.             try
  5.             {
  6.                 strArrSplitCols = strHiddenCols.Split('^');
  7.                 if (strHiddenCols.Trim() != "")
  8.                 {
  9.                     for (int intLoop = 0; intLoop < strArrSplitCols.Length; intLoop++)
  10.                     {
  11.                         myGrid.Columns[Convert.ToInt32(strArrSplitCols[intLoop])].Visible = false;
  12.                     }
  13.                 }
  14.                 myGrid.SelectAll();
  15.                 if (myGrid.Columns.Count != strHiddenCols.Length-1)
  16.                 {
  17.                     Clipboard.SetDataObject(myGrid.GetClipboardContent(), false);
  18.                     ApplicationClass ExcelApp;
  19.                     ExcelApp = new ApplicationClass();
  20.                     ExcelApp.Application.Workbooks.Add(true);
  21.                     ExcelApp.Visible = true;
  22.                     Worksheet ws = new Worksheet();
  23.                     ws = (Worksheet)ExcelApp.ActiveSheet;
  24.                     int colCount = 0;
  25.                     string[] strArrCompany;
  26.                     string strCompanyDtls = iSQL.retrieveRecords("header,telephone,fax,email,website", "company", "company_id=1");
  27.                     strCompanyDtls = strCompanyDtls.Substring(0, strCompanyDtls.Length - 1);
  28.                     strArrCompany = strCompanyDtls.Split('^');
  29.                     ExcelApp.Cells[1, 2] = "Company : " + strArrCompany[0];
  30.                     ExcelApp.Cells[1, 4] = "Phone : " + strArrCompany[1];
  31.                     ExcelApp.Cells[1, 6] = "Fax : " + strArrCompany[2];
  32.                     ExcelApp.Cells[1, 8] = "Email : " + strArrCompany[3];
  33.                     ExcelApp.Cells[1, 10] = "Website : " + strArrCompany[4];
  34.                     ExcelApp.Cells[3, 2] = "Employee : " + General.EMPLOYEE_NAME;
  35.                     ExcelApp.Cells[5, 6] = strHeading;
  36.                     for (int intLoop = 0; intLoop < myGrid.ColumnCount; intLoop++)
  37.                     {
  38.                         if (myGrid.Columns[intLoop].Visible)
  39.                         {
  40.                             ExcelApp.Cells[7, colCount + 2] = myGrid.Columns[intLoop].HeaderText;
  41.                             ExcelApp.Cells.Font.Bold = true;
  42.                             colCount += 1;
  43.                         }
  44.                     }
  45.  
  46.                     ws.Paste(ws.Cells[9, 1], Clipboard.GetText());
  47.                     if (strHiddenCols.Trim() != "")
  48.                     {
  49.                         for (int intLoop = 0; intLoop < strArrSplitCols.Length; intLoop++)
  50.                         {
  51.                             myGrid.Columns[Convert.ToInt32(strArrSplitCols[intLoop])].Visible = true;
  52.                         }
  53.                     }
  54.                 }
  55.                 else
  56.                 {
  57.                     if (strHiddenCols.Trim() != "")
  58.                     {
  59.                         for (int intLoop = 0; intLoop < strArrSplitCols.Length; intLoop++)
  60.                         {
  61.                             myGrid.Columns[Convert.ToInt32(strArrSplitCols[intLoop])].Visible = true;
  62.                         }
  63.                     }
  64.                 }
  65.  
  66.  
  67.             }
  68.             catch (Exception ex)
  69.             {
  70.                 General.writeErrorLog("ExcelExport", "exportToExcel", ex.Message);
  71.             }
  72.         }
  73.  
Feb 4 '08 #1
1 1458
kenobewan
4,871 Recognized Expert Specialist
The ColumnWidth property may work for you. HTH.
Feb 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
5273
by: Niyazi | last post by:
Hi, I have to retrieve a data from AS400 DB2 and after working with data I have to export into one of existing Excel file. I can connect into specific library in AS400 DB2 using AS400...
1
1619
by: Mustafa | last post by:
I have an ASP script where i am generating the excell sheet dynamically i am passing some column header text which is long text so i want it to display it vertically in column (cell).In excell i...
1
1372
by: Mustafa | last post by:
I have an ASP script where i am generating the excell sheet dynamically i am passing text as column heading which is long text so i want it to display it vertically in column (cell).In excell i...
2
1956
by: nivas.meda | last post by:
Hi, I have an excel sheet with a graph and cells.If i change the value in the excel cells the graph will reflect.Now i am going to implement this functionality in html page.I successfully saved...
9
1910
by: Hi5 | last post by:
Hi, I have designed a databasewhich is now able to store all data from my client's Excel sheet.Now I am looking for a good way to move their data into this Db. If I want to do this myself will...
2
2029
seshu
by: seshu | last post by:
hi every body This is seshu i have doubt in mysql i have some date in excell sheet to export that data i have copied all the data into a text file and the wrote this code in...
2
2666
by: glibo | last post by:
I am trying to change the tab name when i open a excell spread sheet on the run time in c#. I changed it in the code: Response.AddHeader("content-disposition","filename=" + sFileName + ".xls");...
1
1445
Ali Rizwan
by: Ali Rizwan | last post by:
Hi all, I m creating a database. The data for database will fetched from an excell sheet. Now how can i read an excell sheet and update my database with that excell sheet. Or I want to show...
3
4560
by: ashokd001 | last post by:
Hi, How do i read time field from excell sheet by python. I am getting "0.400694444444" value but sheet has "9:37:00 AM" . How to convert it ? Regards, Ashok
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.