2011年5月21日 星期六

C# 產生Excel檔

更多資訊可以參考:
http://www.codeproject.com/office/fasterexcelaccesstoc.asp
以及:
http://www.microsoft.com/downloads/details.aspx?familyid=3C9A983A-AC14-4125-8BA0-D36D67E0F4AD&displaylang=en

 這似乎是讀檔:
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
//using Excel;
namespace cs_using_excel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string Path = @"c:\test.xls";
// initialize the Excel Application class
Microsoft.Office.Interop.Excel.ApplicationClass app = new ApplicationClass();
// create the workbook object by opening the excel file.

Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(Path,
0,
true,
5,
"",
"",
true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t",
false,
false,
0,
true,
1,
0);
// get the active worksheet using sheet name or active sheet
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
int index = 0;
// This row,column index should be changed as per your need.
// i.e. which cell in the excel you are interesting to read.
object rowIndex = 2;
object colIndex1 = 1;
object colIndex2 = 2;
try
{
while (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
{
rowIndex = 2 + index;
string firstName = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
string lastName = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();
Console.WriteLine("Name : {0},{1} ", firstName, lastName);
index++;
}
}
catch (Exception ex)
{
app.Quit();
Console.WriteLine(ex.Message);
}
}
}
}
這似乎是寫檔:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;
using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace test_cs_console
{
    class Program
    {
        public static void Main(string[] args)
        {

            #region Uncomment the code in this region to run the TimedAccess class
            // TimedAccess timedAccess = new TimedAccess();
            // timedAccess.Read();
            // return;
            #endregion

            ApplicationClass app = new ApplicationClass();
            Workbook book = null;
            Worksheet sheet = null;
            Range range = null;

            try
            {
                app.Visible = false;
                app.ScreenUpdating = false;
                app.DisplayAlerts = false;

                string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

                //book = app.Workbooks.Open(execPath + @"\..\..\Book1.xls", Missing.Value, Missing.Value, Missing.Value
                //                                  , Missing.Value, Missing.Value, Missing.Value, Missing.Value
                //                                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value
                //                                , Missing.Value, Missing.Value, Missing.Value);
                book = app.Workbooks.Add(Missing.Value);
                sheet = (Worksheet)book.Worksheets[1];

                sheet.get_Range("A1", "A1").Value2="啦啦";

                book.SaveAs(execPath + @"\..\..\test.xls", Missing.Value, Missing.Value, Missing.Value
                                                  , Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value
                                                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                range = null;
                sheet = null;
                if (book != null)
                    book.Close(false, Missing.Value, Missing.Value);
                book = null;
                if (app != null)
                    app.Quit();
                app = null;
            }

        }
    }
}

沒有留言:

張貼留言