locked
C# Excel programming

    Pregunta

  • I can't find anything on how to insert comment(notes) in an Excel worksheet using C#. Any ideas?
    lunes, 16 de octubre de 2006 13:24

Respuestas

  • You simply use the .Addcomment function.



    Microsoft.Office.Interop.Excel.Application excelAPP = new Microsoft.Office.Interop.Excel.ApplicationClass();
    Microsoft.Office.Interop.Excel.Workbook wbk = excelAPP.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

    excelApp.ActiveCell.Select("A1");
    excelApp.ActiveCell.AddComment("Horses");

     

    lunes, 16 de octubre de 2006 17:15
  • You can definitely insert headers and footers into excel via the Excel Interop. You must use the PageSetup propertly declared beneath the sheets class.

    You'll have to declare your worksheet:



    Microsoft.Office.Interop.Excel.Application excelAPP = new Microsoft.Office.Interop.Excel.ApplicationClass();

    Microsoft.Office.Interop.Excel.Workbook wbk = excelAPP.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

    Microsoft.Office.Interop.Excel.Worksheet myWS;
    myWS = (Worksheet)wbk.Worksheets[1];

    myWS.PageSetup.CenterHeader = "Test";

    excelAPP.Visible = true;

     


    You'll see that you have a center, right, and left header/footer that you can modify. I went ahead and threw this into a blank c# project to test and it did work properly.
    lunes, 16 de octubre de 2006 20:36

Todas las respuestas

  • You simply use the .Addcomment function.



    Microsoft.Office.Interop.Excel.Application excelAPP = new Microsoft.Office.Interop.Excel.ApplicationClass();
    Microsoft.Office.Interop.Excel.Workbook wbk = excelAPP.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

    excelApp.ActiveCell.Select("A1");
    excelApp.ActiveCell.AddComment("Horses");

     

    lunes, 16 de octubre de 2006 17:15
  • Thanks that helped!

     

    Any ideas on how to insert headers and footers?

    lunes, 16 de octubre de 2006 18:08
  • You can definitely insert headers and footers into excel via the Excel Interop. You must use the PageSetup propertly declared beneath the sheets class.

    You'll have to declare your worksheet:



    Microsoft.Office.Interop.Excel.Application excelAPP = new Microsoft.Office.Interop.Excel.ApplicationClass();

    Microsoft.Office.Interop.Excel.Workbook wbk = excelAPP.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

    Microsoft.Office.Interop.Excel.Worksheet myWS;
    myWS = (Worksheet)wbk.Worksheets[1];

    myWS.PageSetup.CenterHeader = "Test";

    excelAPP.Visible = true;

     


    You'll see that you have a center, right, and left header/footer that you can modify. I went ahead and threw this into a blank c# project to test and it did work properly.
    lunes, 16 de octubre de 2006 20:36
  • mEt, I was having trouble running your sample code, I did my open WorkSheet a little differently, can you give a little more details so I can run it?
    lunes, 16 de octubre de 2006 21:45
  • Sure, if you just want to let me know how  you opened your worksheet and I'll give you the exact code.  Keep in mind you won't see the header on your normal worksheet, you have to go into file, page setup and then check the headers from there. 
    lunes, 16 de octubre de 2006 22:02
  • mEt

    My program is a bit convoluted. I'd appreciatate if you can post your full sample program, so I may attempt to follow it.

    martes, 17 de octubre de 2006 14:05
  • mEt

    I got my sample program to work with your example. Do you know how to set fonts for the headers?

    martes, 17 de octubre de 2006 15:23
  •  Nate00 wrote:

    Do you know how to set fonts for the headers?



    Nate00,

    The only  way I know how to set the font for the header is by using the string builder located in the System.Text namespace.  Below is the added string builder to the existing cod I gave you:



    private void button1_Click(object sender, EventArgs e)
            {
                Microsoft.Office.Interop.Excel.Application excelAPP = new Microsoft.Office.Interop.Excel.ApplicationClass();

                Microsoft.Office.Interop.Excel.Workbook wbk = excelAPP.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

                Microsoft.Office.Interop.Excel.Worksheet myWS;
                myWS = (Worksheet)wbk.Worksheets[1];

                StringBuilder str = new StringBuilder();
                str.Append(@"&""Arial Unicode MS,Regular""Test");

                myWS.PageSetup.CenterHeader = str.ToString();

                excelAPP.Visible = true;
            }

     

    miércoles, 18 de octubre de 2006 20:21
  • Is the format for the font string described anywhere? How would I add a font size to the string you have shown.

    Thanks


    Tom G.

    martes, 11 de septiembre de 2012 21:14
  • I know this post is old, but for the future benefit of others, maybe you should consider something like this:

    using Excel Microsoft.Office.Interop.Excel;
    public class 
    Form1 {
        
        
    // ~~> Define your Excel Objects
        
    private Excel.Application xlApp = new Excel.Application();
        
        private 
    Excel.Workbook xlWorkBook;
        
        private 
    Excel.Worksheet xlWorkSheet;
        
        private 
    Excel.Range xlRng;
        
        private void 
    Button1_Click(object sender, System.EventArgs e) {
            
    // ~~> Add a New Workbook
            
    xlWorkBook xlApp.Workbooks.Add;
            
    // ~~> Set reference to Sheet1
            
    xlWorkSheet xlWorkBook.Sheets(Sheet13);
            
    // ~~> Set reference to the range that we would be working with
            
    xlRng xlWorkSheet.Range(D103);
            
    // ~~> Display Excel
            
    xlApp.Visible = true;
            string 
    msgComment "Hello! This is a Sample Comment";
            
    xlRng.AddComment(msgComment);
            
    // ~~> Save the file
            
    xlWorkBook.SaveAs("C:\\SampleNew.xlsx"51);
            
    // TODO: Labeled Arguments not supported. Argument: 1 := 'Filename'
            // TODO: Labeled Arguments not supported. Argument: 2 := 'FileFormat'
            // ~~> Close the File
            
    xlWorkBook.Close();
            
    // ~~> Quit the Excel Application
            
    xlApp.Quit();
            
    // ~~> Clean Up
            
    releaseObject(xlApp);
            
    releaseObject(xlWorkBook);
        
    }
        
        
    // ~~> Release the objects
        
    private void releaseObject(object obj) {
            
    try {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
    ;
                
    obj = null;
            
    }
            
    catch (Exception ex) {
                obj 
    = null;
            
    }
            
    finally {
                GC.Collect()
    ;
            
    }
        }
    }


    Ryan Shuell

    miércoles, 12 de diciembre de 2012 5:26