Forum Sementara Putera.com

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Forum Sementara Putera.com

Bersama kita perkemaskan forum ini sementara forum asal dalam pemulihan.

Forum putera dah kembali. Masalah sudah berjaya diselesaikan. Sila lawati http://forum.putera.com/tanya


2 posters

    C#.Net Programme convert to Excel 2007

    avatar
    rshin2020
    Ahli Baharu
    Ahli Baharu


    Number of posts : 3
    Registration date : 08/01/2010

    C#.Net Programme convert to Excel 2007 Empty C#.Net Programme convert to Excel 2007

    Post by rshin2020 Fri Jan 08, 2010 2:28 pm

    Hi. I have to write a simple program to display a console application for:
    String - Voltage, EmployeeID
    Number - 1.016, 00180000
    Here's what ive done:


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {

    static void Main(string[] args)
    {
    double p = 1.016;
    int q = 00180000;

    Console.WriteLine("Voltage: {0}", p);
    Console.WriteLine("EmployeeID: {1}", q);
    }


    }
    }


    I seem to get an error but don't know where the error is. Could someone please help me to correct the code. I also need to convert the data to be displayed in Excel 2007. How do I do that. I'm really new to C#.net. Please help, thanks.
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    C#.Net Programme convert to Excel 2007 Empty Re: C#.Net Programme convert to Excel 2007

    Post by johnburn Fri Jan 08, 2010 4:33 pm

    To create Excel file, you can use System.IO.Packaging classes (.NET 3.0).
    This might give you some ideas:
    http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c13123/
    avatar
    rshin2020
    Ahli Baharu
    Ahli Baharu


    Number of posts : 3
    Registration date : 08/01/2010

    C#.Net Programme convert to Excel 2007 Empty Re: C#.Net Programme convert to Excel 2007

    Post by rshin2020 Fri Jan 08, 2010 5:05 pm

    Based on the code below, which part do I change? And from what I have read, it said that: 'Start by adding a new worksheet called "Tinned Goods" and adding some basic data and a simple calculation'. Meaning, do i have to type an example in a spreadsheet in excel? Or how?


    using OfficeOpenXml; // namespace for the ExcelPackage assembly
    b&
    FileInfo newFile = new FileInfo(@"C:\mynewfile.xlsx");
    using (ExcelPackage xlPackage = new ExcelPackage(newFile)) { b& }


    ExcelWorksheet worksheet =
    xlPackage.Workbook.Worksheets.Add("Tinned Goods");
    // write some titles into column 1
    worksheet.Cell(1, 1).Value = "Product";
    b&
    worksheet.Cell(4, 1).Value = "Peas";
    worksheet.Cell(5, 1).Value = "Total";

    // write some values into column 2
    worksheet.Cell(1, 2).Value = "Tins Sold";

    ExcelCell cell = worksheet.Cell(2, 2);
    cell.Value = "15"; // tins of Beans sold
    string calcStartAddress = // we want this for the formula
    cell.CellAddress;
    worksheet.Cell(3, 2).Value = "32"; // tins of Carrots sold
    b&
    worksheet.Cell(5, 2).Formula = string.Format(SUM({0}:{1})",
    calcStartAddress, calcEndAddress);

    Hope can help. Thanks.
    avatar
    rshin2020
    Ahli Baharu
    Ahli Baharu


    Number of posts : 3
    Registration date : 08/01/2010

    C#.Net Programme convert to Excel 2007 Empty Re: C#.Net Programme convert to Excel 2007

    Post by rshin2020 Tue Jan 12, 2010 5:20 pm

    Still no reply. Anyway, here's what i have done. I need it in a console format. How do I do it using this code?

    using System;
    using System.Collections;
    using System.Reflection; // For Missing.Value and BindingFlags
    using System.Runtime.InteropServices; // For COMException
    using Excel = Microsoft.Office.Interop.Excel;
    using System.ComponentModel;
    using System.Data;




    namespace MSExcelApp
    {
    /// <summary>
    /// Summary description for ExcelAuto.
    /// </summary>
    public class ExcelAuto
    {
    private Excel.Application ExcelApp;
    private Excel.Workbook objBook;
    private Excel.Worksheet objSheet;
    private Excel.Range range;
    string strAttendeeList, strAbsenteeList, strCopiesToList;
    int totActionItems;
    object oMissing, oTemplate;
    private string strTitle;

    public ExcelAuto()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    public void CreateFile(ArrayList array)
    {
    object missing = System.Reflection.Missing.Value;
    object fileName = "normal.dot";
    object newTemplate = false;
    object docType = 0;
    object isVisible = true;

    ExcelApp = new Excel.ApplicationClass();
    ExcelApp.Visible = true;
    objBook = ExcelApp.Workbooks.Add(missing);
    objSheet = (Excel.Worksheet)objBook.Sheets["Sheet1"];
    objSheet.Name = "It's Me";

    objSheet.Cells[1, 1] = "Details";
    objSheet.Cells[2, 1] = "Voltage : "+ array[0].ToString();
    objSheet.Cells[3, 1] = "EmployeeID : "+ array[1].ToString();

    objSheet.get_Range("A1", "A1").Font.Bold = true;
    objSheet.get_Range("A1", "A6").EntireColumn.AutoFit();
    objSheet.get_Range("A1","A7").BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlMedium,
    Excel.XlColorIndex.xlColorIndexAutomatic,Excel.XlColorIndex.xlColorIndexAutomatic);
    }

    static void Main()
    {
    Application.Run(new Form1());
    }

    private void btnCreate_Click(object sender, System.EventArgs e)
    {
    ExcelAuto excel = new ExcelAuto();
    ArrayList array = new ArrayList();
    bool filled = true;

    if ( ( 1.016).ToString().Length.Equals(0) )
    {
    errorProvider.SetError(1.016,"cannot be empty" );
    filled = false;
    }
    if( ( 00180000 ).ToString().Length.Equals(0) )
    {
    errorProvider.SetError( 00180000,"cannot be empty" );
    filled = false;
    }


    if ( filled == true )
    {
    array.Insert(0,1.016);
    array.Insert(1,00180000);


    excel.CreateFile(array);
    }
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    Application.Exit();
    }
    }
    }

    Sponsored content


    C#.Net Programme convert to Excel 2007 Empty Re: C#.Net Programme convert to Excel 2007

    Post by Sponsored content


      Current date/time is Sun May 19, 2024 9:34 pm