Skip to content

Latest commit

 

History

History
127 lines (106 loc) · 6.66 KB

README.md

File metadata and controls

127 lines (106 loc) · 6.66 KB

xlsf_with_C_sharp

A class of xls file with C#

想要仿照 xlsf_with_MFC的class使用方式 做一個C#版本的class

xls file with C sharp

xls file with C#

聲明

這是一份學C#沒多久,而寫出來的程式碼。

最大目的希望可以讓目前使用C#的人,可以更愉快一點!^^"

希望這個Class可以拋磚引玉,引起大家對於C#使用心得的話題,討論聊C#的使用心得。

作用

從C#加入Excel的參考,簡化它的function name

使用平台

  • OS: Windows 8.1
  • Tool: Visual Studio 2013
  • Reference: Excel 2013(其他版本沒有測試過)

檔案說明

  • main.cs Demo功能的主程式
  • xlsf.cs 定義檔

參考資料

Sample Code

用起來的code會像這樣

namespace XlsFile
{
    class Program
    {
        static void Main(string[] args)
        {
            xlsf excel_file = new xlsf();
            //xlsf excel_file = new xlsf(Marshal.GetActiveObject("Excel.Application"));

            //excel_file.NewFile();
            excel_file.OpenFile(@"C:\活頁簿1.xlsx");

            excel_file.SetVisible(true);

            excel_file.SelectCell("A1").SetCell("ID Number");
            excel_file.SelectCell("A1").AutoFitWidth();

            excel_file.SelectCell("B", 1).SetCell("Current Balance");
            excel_file.SelectCell("B", 1).AutoFitWidth();

            excel_file.SelectCell("A2").SetCellColor(Color.Beige);

            excel_file.SelectCell("A3", "B4").SetCellColor(Color.Black);
            excel_file.OffsetSelectCell(1, 2).SetCell("Black offset 1, 2");
            excel_file.AutoFitHight();

            excel_file.NewSheet();
            excel_file.SelectCell("B1").SetCell("this is B1");
            excel_file.SelectCell("B1").CopyCell("B1", "B5");

            excel_file.SelectCell("C1").SetCell("2016/3/16");
            excel_file.SelectCell("C1").SetFont("Console").SetFontSize(42).SetFontColor(Color.Blue).SetCellBk(Color.Orange);
            string datetime = excel_file.SelectCell("C1").GetCell2DateTime().ToString();
            Console.WriteLine(datetime);
            excel_file.SelectCell("D1").SetCell("this is D1");

            Console.WriteLine("SheetTotal:{0}", excel_file.SheetTotal());
            Console.WriteLine("SheetName:{0}", excel_file.GetSheetName());
            excel_file.SelectSheet(@"工作表1").SetSheetName("new sheet name");
            Console.WriteLine("SheetName:{0}", excel_file.GetSheetName());

            excel_file.SelectSheet(@"工作表3").CopySheet();
            excel_file.SelectCell("A1").SetCell(1);
            excel_file.SelectCell("A2").SetCell(2);
            excel_file.SelectCell("A3").SetCell(3);
            excel_file.SelectCell("A4").SetCell(4);
            excel_file.SelectCell("A4").SetCellHeight(36).SetCellWidth(68);
            excel_file.SelectCell("A5").SetCell("=SUM(A1:A4)");
            string str = excel_file.SelectCell("A5").GetCell2Str();
            Console.WriteLine(str);
            long number = excel_file.SelectCell("A5").GetCell2Int();
            Console.WriteLine(number);
            excel_file.SelectCell("A5").SetFontBold(true).SetFontStrkthrgh(true);
            excel_file.SelectSheet(2).CopySheet();
            //excel_file.DeleteSheet(2);

            Console.WriteLine("SheetTotal:{0}", excel_file.SheetTotal());
            excel_file.SelectSheet(@"工作表3").MoveSheet();
            //excel_file.SaveAs(@"C:\321.xlsx");
            //excel_file.CloseFile(false);
            //excel_file.Quit();
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}