2. Cells of A1 ~ G1 merge
3. wrtie text "C# Excel Creation"
4. set bold font
5. draw the box line
IDE : Microsoft Visual Studio 2005
At first, include Excel Library.
full source
=================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ExcelExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//create Excel application
Excel.Application oXL = new Excel.Application();
//if set true, can see excel doc during creating
oXL.Visible = true;
//must option. if set ture, it is possible to break from user's action
oXL.Interactive = false;
Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
Excel.Range oRng = null;
//get cells
oRng = oSheet.get_Range("B2", "H2");
oRng.MergeCells = true;
//set text
oRng.Value2 = "C# Excel Creation";
oRng.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
oRng.Font.Bold = true;
oRng.Font.Color = -16776961;
//box line
oRng.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
oRng.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
oRng.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
oRng.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
oXL.Interactive = true;
//clear object
Marshal.ReleaseComObject(oXL);
Marshal.ReleaseComObject(oWB);
Marshal.ReleaseComObject(oSheet);
//excute GC
GC.Collect();
}
catch (Exception eExcep)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, eExcep.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, eExcep.Source);
MessageBox.Show(errorMessage.ToString());
}
}
}
}
댓글 없음:
댓글 쓰기