how to set properties to excell sheet by c#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    how to set properties to excell sheet by c#?

    hai all,
    i want to set width, alignment for excell sheet columns programatically through c#,
    how to do this?
    my coding
    Code:
     public  void exportToExcel(DataGridView myGrid, string strHiddenCols,string strHeading)
            {
                string[] strArrSplitCols;
                try
                {
                    strArrSplitCols = strHiddenCols.Split('^');
                    if (strHiddenCols.Trim() != "")
                    {
                        for (int intLoop = 0; intLoop < strArrSplitCols.Length; intLoop++)
                        {
                            myGrid.Columns[Convert.ToInt32(strArrSplitCols[intLoop])].Visible = false;
                        }
                    }
                    myGrid.SelectAll();
                    if (myGrid.Columns.Count != strHiddenCols.Length-1)
                    {
                        Clipboard.SetDataObject(myGrid.GetClipboardContent(), false);
                        ApplicationClass ExcelApp;
                        ExcelApp = new ApplicationClass();
                        ExcelApp.Application.Workbooks.Add(true);
                        ExcelApp.Visible = true;
                        Worksheet ws = new Worksheet();
                        ws = (Worksheet)ExcelApp.ActiveSheet;
                        int colCount = 0;
                        string[] strArrCompany;
                        string strCompanyDtls = iSQL.retrieveRecords("header,telephone,fax,email,website", "company", "company_id=1");
                        strCompanyDtls = strCompanyDtls.Substring(0, strCompanyDtls.Length - 1);
                        strArrCompany = strCompanyDtls.Split('^');
                        ExcelApp.Cells[1, 2] = "Company : " + strArrCompany[0];
                        ExcelApp.Cells[1, 4] = "Phone : " + strArrCompany[1];
                        ExcelApp.Cells[1, 6] = "Fax : " + strArrCompany[2];
                        ExcelApp.Cells[1, 8] = "Email : " + strArrCompany[3];
                        ExcelApp.Cells[1, 10] = "Website : " + strArrCompany[4];
                        ExcelApp.Cells[3, 2] = "Employee : " + General.EMPLOYEE_NAME;
                        ExcelApp.Cells[5, 6] = strHeading;
                        for (int intLoop = 0; intLoop < myGrid.ColumnCount; intLoop++)
                        {
                            if (myGrid.Columns[intLoop].Visible)
                            {
                                ExcelApp.Cells[7, colCount + 2] = myGrid.Columns[intLoop].HeaderText;
                                ExcelApp.Cells.Font.Bold = true;
                                colCount += 1;
                            }
                        }
    
                        ws.Paste(ws.Cells[9, 1], Clipboard.GetText());
                        if (strHiddenCols.Trim() != "")
                        {
                            for (int intLoop = 0; intLoop < strArrSplitCols.Length; intLoop++)
                            {
                                myGrid.Columns[Convert.ToInt32(strArrSplitCols[intLoop])].Visible = true;
                            }
                        }
                    }
                    else
                    {
                        if (strHiddenCols.Trim() != "")
                        {
                            for (int intLoop = 0; intLoop < strArrSplitCols.Length; intLoop++)
                            {
                                myGrid.Columns[Convert.ToInt32(strArrSplitCols[intLoop])].Visible = true;
                            }
                        }
                    }
                   
    
                }
                catch (Exception ex)
                {
                    General.writeErrorLog("ExcelExport", "exportToExcel", ex.Message);
                }
            }
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    The ColumnWidth property may work for you. HTH.

    Comment

    Working...