Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > C# .NET Generate Excel (Windows 7 and Office Excel 2003 , Excel 2007 , Excel 2010)




Clound SSD Virtual Server

C# .NET Generate Excel (Windows 7 and Office Excel 2003 , Excel 2007 , Excel 2010)

 
  C# .NET Generate Excel (Windows 7 and Office Excel 2003 , Office Excel 2007, Office Excel 2010) ตัวอย่างการใช้ C# ในการสร้างเอกสาร Excel (.xls) โดยใช้ Class Library ของ Microsoft.Office.Interop.Excel สำหรับตัวอย่างนี้ได้ทำการทดสอบบน Windows 7 และ Office 2007 สามารถทำงานได้ปกติ

และสามารถใช้ได้ทั้ง Application ที่เป็น Windows Application (Win App) , Web Application (ASP.NET) และ Console App หรืออื่น ๆ

สำหรับบทความการใช้ ASP.NET กับ Excel และอื่น ๆ ตัวอย่างการใช้ สามารถอ่านได้จากหัวข้อ ASP.NET กับ Excel (VB.NET)


จากบทความลิ้งค์ก่อนหน้านี้จะเป็นภาษา VB.NET ซึ่งในตัวอย่างได้มีการเรียกใช้งาน Property หลายตัว ซึ่งสามารถนำมาดัดแปลงได้กับภาษา C# ได้อย่างไม่ยาก


C# กับ Excel

เริ่มต้นด้วยการ Add Reference ตัว Library ชื่อ Microsoft Excel 12.0 Object Library หรือ Version อื่น ๆ ที่ได้ติดตั้งลงในเครื่อง (Office Excel)

C# .NET  Excel 1

คลิกขวาที่ Project เลือก Add Reference

C# .NET Excel 2


เลือก Microsoft Excel 12.0 Object Library เหมือนในรูป

เพิ่มเติม สำหรับ Excel 2010 ให้ใช้ Microsoft Excel 14.0 Object Library

Excel 2010

C# .NET Excel 3

Library ถูก Add ลงเรียบร้อยแล้ว

ตัวอย่าง Code ของภาษา C#

frmExcel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel; 

namespace ExcelC
{
    public partial class frmExcel : Form
    {
        public frmExcel()
        {
            InitializeComponent();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object mis = Type.Missing;

            string strFileName = "C:\\ExcelC\\Xls\\GenExcel.xls";

            xlApp = new Excel.ApplicationClass();
            //xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(mis);
     
            //xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Add(mis, mis, mis, mis); /*** for Add New Sheet ***/
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Name = "My Sheet";
            xlWorkSheet.Cells[1, 1] = "www.ThaiCreate.Com";
            xlWorkSheet.Cells[2, 1] = "Mr.Weerachai Nukitram";

            xlWorkBook.SaveAs(strFileName,
                mis, mis, mis, mis, mis, Excel.XlSaveAsAccessMode.xlExclusive, mis, mis, mis, mis, mis);
            xlWorkBook.Close(true, mis, mis);
            xlApp.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);

            xlApp = null;
            xlWorkBook = null;
            xlWorkSheet = null;
            GC.Collect();

            MessageBox.Show("Generate Successfully");

        }

    }
}


Screenshot

C# .NET Excel 4


C# .NET Excel 5









เพิ่มเติมเกี่ยวกับเรื่อง DCOM + Permission

กรณีที่ Error นี้
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.


ถ้าเป็น Windows XP + Office Excel 2003 อ่านได้ที่นี่


DCOM Config (Windows 7 + Office Excel 2007)

Control Panel
Control Panel\All Control Panel Items\Administrative Tools\Event Viewer


Event Viewer

DCOM Config Windows 7 and Office Excel 2007

สังเกตุว่ามี Event Viewer มี Error Log ชื่อว่า DistributedCOM

DCOM Config Windows 7 and Office Excel 2007

รายละเอียดของ Error
The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID
{00024500-0000-0000-C000-000000000046}
and APPID
{00020812-0000-0000-C000-000000000046}
to the user WEERACHAI-NB\WEERACHAI SID (S-1-5-21-1205256255-3473717987-3694456053-1001) from address LocalHost (Using LRPC). This security permission can be modified using the Component Services administrative tool.


จะมี User ชื่อว่า WEERACHAI ติด Permission อยู่ เพราะเป็น Windows Form Application จึงใช้ User ที่เป็น Logon User ในการรัน แต่ถ้าเป็น ASP.NET ที่รันด้วย IIS จะใช้ User ว่า IUSR_WEERACHAI และ IWAM_WEERACHAI








การแก้ปัญหา

Component Services
Control Panel\All Control Panel Items\Administrative Tools\Component Services


DCOM Config Windows 7 and Office Excel 2007

เปิดโปรแกรม Component Services และคลิกเลือกที่ DCOM Config

DCOM Config Windows 7 and Office Excel 2007

คลิกขวาที่ Microsoft Excel Application -> Properties

เพิ่มเติมสำหรับเเครื่อง 64-Bit

64-Bit Excel

ให้รัยคำสั่ง C:\WINDOWS\SysWOW64>mmc comexp.msc /32

64-Bit Excel

DCOM Config Windows 7 and Office Excel 2007

ตรง Tab Security และ Launch and Activation Permission ให้เลือก Customize -> Edit ตามรูป

DCOM Config Windows 7 and Office Excel 2007

เลือกที่ Add

DCOM Config Windows 7 and Office Excel 2007

คลิกที่ Advanced

DCOM Config Windows 7 and Office Excel 2007

Find Now และเลือก User ที่ติด Permission ซึ่งในตอนแรกชื่อว่า WEERACHAI

DCOM Config Windows 7 and Office Excel 2007

เลือก OK

DCOM Config Windows 7 and Office Excel 2007

กำหนดค่าดังรูป

DCOM Config Windows 7 and Office Excel 2007

Tab Identity ให้เลือก The interactive user


หลังจากเรียบร้อยแล้ว ก็ให้ทำการทดสอบ Run โปรแกรมใหม่อีกครั้ง และถ้ายังไม่ได้ ให้ลองสังเกตุดูที่ Event Viewer ว่ามี Log แจ้ง Error หรือไม่

ตอนที่ 2 : C# Open Excel and Create Font,Border,Color,Style




       
Bookmark.   
       

 

  By : TC Admin
  Score Rating : -
  Create Date : 2011-09-07 16:11:48
  Download : No files
     

Clound SSD Virtual Server
-->
Related Links
.NET Smart Device  เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window  Mobile 5-6, Hand Held,...
.NET Smart Device เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window Mobile 5-6, Hand Held,...
.NET Smart Device Project เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window Mobile 5-6, Hand Held,...
Rating :
Update :
2017-03-24 21:17:48 View : 41,459
Crystal Report กับการสร้าง Report รายงานแบบ JOIN ข้อมูลหลายตาราง Table (.NET)
Crystal Report กับการสร้าง Report รายงานแบบ JOIN ข้อมูลหลายตาราง Table (.NET)
ตัวอย่างการออก report ของ crystal report ที่มีความซับซ้อนยิ่งขึ้น และการนำข้อมูลหลาย ๆ ตารางมาแสดง ใน report เดียวกัน
Rating :
Update :
2017-03-17 21:20:01 View : 65,452
.NET Windows Form กับการส่งอีเมล์ (Send Mail) แบบง่าย ๆ ด้วย System.Net.Mail (VB.NET , C#)
.NET Windows Form กับการส่งอีเมล์ (Send Mail) แบบง่าย ๆ ด้วย System.Net.Mail (VB.NET , C#)
ง่าย ๆ กับ .NET Framework การเขียน Windows Form ส่งอีเมล์ แบบง่าย ๆ ด้วย Namespace ของ System.Net.Mail
Rating :
Update :
2017-03-17 22:15:41 View : 18,250
VB.NET/C# Login Check Username Password
VB.NET/C# Login Check Username Password
สร้างระบบล็อกอิน Login Form ตรวจสอบสิทธิ์ของ Username และ Password ก่อนจะเข้าสู่โปรแกรม
Rating :
Update :
2012-02-27 13:25:22 View : 26,142
GridView Ajax and jQuery การสร้าง GridView  บน ASP.NET เพื่อเรียกใช้งาน Ajax กับ jQuery
GridView Ajax and jQuery การสร้าง GridView บน ASP.NET เพื่อเรียกใช้งาน Ajax กับ jQuery
ASP.NET ตัวอย่างการสร้าง GridView และการเรียกใช้งาน Ajax ด้วย jQuery เพื่อเรียกข้อมูลในแต่ละแถวของ GridView แบบง่าย ๆ
Rating :
Update :
2017-03-24 21:30:30 View : 16,622
Basic ASP.NET jQuery Framework (พื้นฐานง่าย ๆ ด้วย ASP.NET กับ jQuery)
Basic ASP.NET jQuery Framework (พื้นฐานง่าย ๆ ด้วย ASP.NET กับ jQuery)
บทความ ASP.NET กับ jQuery พื้นฐานการเขียนโปรแกรม ASP.NET jQuery บน Visual Studio กับ JScript jQuery IntelliSense
Rating :
Update :
2017-03-24 21:22:21 View : 24,222
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่

Inline