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,007

HOME > .NET Framework > C# (.Net) Open Excel Template and Create Font,Border,Color,Style (Office 2003,2007,2010)




Clound SSD Virtual Server

C# (.Net) Open Excel Template and Create Font,Border,Color,Style (Office 2003,2007,2010)

 
  C# Open Excel Template and Create Font/Border/Color/Style ตัวอย่างการใช้ C# ในการเปิดไฟล์ Excel ที่มีอยู่ และทำการสร้างเป็น Excel ชุดใหม่ โดยตกแต่งข้อความ เช่น สี , ขนาด ของ ฟอนต์


C# Excel

C# Generate Excel


จากบทความ


บทความนี้จะเป็นการเปิด Excel ที่มีอยู่ และทำการเขียนข้อความลงใน Excel และทำการ Save เป็นไฟล์ใหม่ โดยมีการเพิ่ม ตกแต่งเพิ่มเติม เช่น
xlWorkSheet.get_Range("A3", "I3").Font.Name = "Tahoma";
xlWorkSheet.get_Range("A3", "I3").Font.Size = "15";
// xlWorkSheet.get_Range("A3", "I3").HorizontalAlignment = 4;
xlWorkSheet.get_Range("A3", "I3").VerticalAlignment = -4108;
xlWorkSheet.get_Range("A3", "I3").HorizontalAlignment = -4108;
xlWorkSheet.get_Range("A3", "I3").Interior.ColorIndex = 44;
xlWorkSheet.get_Range("A3", "I3").Font.ColorIndex = 4;
xlWorkSheet.get_Range("A3", "I3").Font.Italic = true;


สำหรับการใช้งาน Property อื่น ๆ สามารถอ่านได้จากบทความ ASP.NET + Excel
Go to : ASP.NET Write Excel Style and Format (Excel Application)
Go to : ASP.NET Excel.Application


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 เหมือนในรูป

C# .NET Excel 3

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

Excel 2010

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








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

C# .NET Excel 6

ไฟล์ต้นฉบับ

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 strTemplate = "C:\\ExcelC\\Xls\\Template.xls";
            string strFileName = "C:\\ExcelC\\Xls\\GenExcel.xls";

            xlApp = new Excel.ApplicationClass();
            //xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(strTemplate, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                 
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            xlWorkSheet.Name = "My Sheet";
            
            xlWorkSheet.Cells[2, 1] = "DataCol 1";
            xlWorkSheet.Cells[2, 2] = "DataCol 2";
            xlWorkSheet.Cells[2, 3] = "DataCol 3";
            xlWorkSheet.Cells[2, 4] = "DataCol 4";
            xlWorkSheet.Cells[2, 5] = "DataCol 5";
            xlWorkSheet.Cells[2, 6] = "DataCol 6";
            xlWorkSheet.Cells[2, 7] = "DataCol 7";
            xlWorkSheet.Cells[2, 8] = "DataCol 8";
            xlWorkSheet.Cells[2, 9] = "DataCol 9";

            xlWorkSheet.get_Range("A3", "I3").Merge(true);
            xlWorkSheet.get_Range("A3", "I3").BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
            xlWorkSheet.get_Range("A3", "I3").Font.Name = "Tahoma";
            xlWorkSheet.get_Range("A3", "I3").Font.Size = "15";
           // xlWorkSheet.get_Range("A3", "I3").HorizontalAlignment = 4;
            xlWorkSheet.get_Range("A3", "I3").VerticalAlignment = -4108;
            xlWorkSheet.get_Range("A3", "I3").HorizontalAlignment = -4108;
            xlWorkSheet.get_Range("A3", "I3").Interior.ColorIndex = 44;
            xlWorkSheet.get_Range("A3", "I3").Font.ColorIndex = 4;
            xlWorkSheet.get_Range("A3", "I3").Font.Italic = true;

            xlWorkSheet.Cells[3, 1] = "Hello Hello Hello Hello";

            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 7


เทคนิคในการเขียนโปรแกรม ถ้าหากเราได้พัฒนาโปรแกรมบน Visual Studio ค่าต่าง ๆ ที่เป็น Property จะแสดงในส่วนของ Auto Complete ซึ่งจะช่วยให้การเขียนโปรแกรมง่ายและสะดวกเป็นอย่างมาก

C# .NET Excel 8


       
Bookmark.   
       

 

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

Clound SSD Virtual Server
-->
Related Links
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 : 23,614
Install .NET Framework 1.1 & Visual Studio .NET 2003
Install .NET Framework 1.1 & Visual Studio .NET 2003
Install .NET Framework 1.1 & Visual Studio .NET 2003 ติดตั้งและใช้งาน .NET Framework 1.1 และ VS 2003
Rating :
Update :
2009-01-11 09:41:25 View : 6,654
สร้าง Sub Report (Subreport) บน Crystal Report แบบ Step by Step (VB.NET /C#)
สร้าง Sub Report (Subreport) บน Crystal Report แบบ Step by Step (VB.NET /C#)
การสร้าง Sub Report บน Crystal Report และการ Link ข้อมูลระหว่าง Main Report กับ Sub Report รวมทั้งการ Set DataSource ให้กับ Sub Report
Rating :
Update :
2017-03-24 21:34:46 View : 51,790
ASP.NET เรียก PHP กับฐานข้อมูล MySQL ผ่าน Web Service และการรับส่งค่าผ่านเว็บเซอร์วิส
ASP.NET เรียก PHP กับฐานข้อมูล MySQL ผ่าน Web Service และการรับส่งค่าผ่านเว็บเซอร์วิส
การสร้างเว็บเซอร์วิสด้วย php กับฐานข้อมูล mysql และทำการเรียก web service ด้วย asp.net ซึ่งอยู่ในฝั่ง client โดยทำการรับส่งข้อมูลระหว่าง php กับ asp.net
Rating :
Update :
2017-03-17 21:21:37 View : 16,474
Basic jQuery for AJAX in ASP.NET Web Site พื้นฐาน Ajax กับ ASP.NET ด้วย jQuery
Basic jQuery for AJAX in ASP.NET Web Site พื้นฐาน Ajax กับ ASP.NET ด้วย jQuery
บทความ jQuery กับการใช้งาน Ajax บน ASP.NET Framework ทำความเข้าใจพื้นฐานการทำงานกับ Ajax ด้วย jQuery กับ ASP.NET แบบง่าย ๆ เบสิก
Rating :
Update :
2017-03-24 21:23:54 View : 20,969
.NET Framework รวบรวมเทคนิคการพัฒนาโปรแกรมด้วย .NET ทั้ง VB.NET และ C#
.NET Framework รวบรวมเทคนิคการพัฒนาโปรแกรมด้วย .NET ทั้ง VB.NET และ C#
หัวข้อนี้ผมได้รวบรมเทคนิคต่าง ๆ ที่เกี่ยวข้องกับการพัฒนาโปรแกรมด้วย .NET Framework ครบคลุมทั้ง Framework 1,2,3 และภาษา VB.NET,C#
Rating :
Update :
2017-03-24 21:23:16 View : 14,826
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
ปรับแต่งเว็บให้โหลดเร็ว


สุดยอด Source Code V2.0
 

แจ้งชำระเงิน/โอนเงิน
 

Hit Link
   







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

Inline