Day: July 13, 2019

  • Gatsby Framework

    Gatsby คืออะไร?

    • เป็น open source framework มีพื้นฐานมาจาก React
    • เป็น Static Site Generator กล่าวคือเมื่อเขียนเว็บ -> Build -> HTML+ css + javascript -> Deploy กับ web server หลายๆเว็บ เช่น apache, firebase, github pages หรือ netlify อื่นๆ อีกหลายเว็บ

    Create Gatsby Project
    ก่อนอื่นเราจะต้องมาติดตั้งตัว gatsby-cli ซึ่งสามารถใช้ได้ทั้งคำสั่ง Yarn หรือ NPM ดังภาพที่ 1

    npm install -g gatsby-cli

    yarn global install gatsby-cli

    ภาพที่ 1 ติดตั้ง gatsby-cli

    เมื่อเราติดตั้ง gatsby-cli ขั้นตอนต่อไปก็จะเป็นการสร้าง website ใหม่ด้วย คำสั่ง

    gatsby new blog

    หลังจากสร้าง blog เรียบร้อยแล้วก็เข้าไปใน project directory ที่สร้างขึ้นมาใหม่ ในที่นี้คือ “blog” แล้วใช้คำสั่ง 

    gatsby develop เพื่อรัน development server

    และเราจะได้ default page ที่ Gatsby สร้างให้ ซึ่งสามารถเข้าถึงเว็บได้ด้วย http://localhost:8000 ดังภาพที่ 2

    ภาพที่ 2 Gatsby Default Starter

    ต่อไปเราจะลองแก้ไข Gatsby Config ในไฟล์ gatsby-config.js แล้วกลับไปยัง Browser ที่เราเปิดไว้ในภาพที่ 2จะได้ผลลัพธ์ดังภาพที่ 3 กล่าวคือเราสามารถแก้ Code แล้วSave หน้าเว็บก็จะอัพเดททันทีโดยที่เราไม่ต้องสั่ง Build ใหม่ เนื่องจาก Gatsby เป็น hot reload ดังนั้นเมื่อเราแก้ไขไฟล์ใดๆ จะทำการ build ให้เราใหม่เสมอ เหมือนรัน develop

    ภาพที่ 3 ผลลัพธ์จากการแก้ไข gatsby-config.js
  • Create Excel 2007 file using Interop services.

    Microsoft Office Interop (Excel Automation)คือ ตัวเลือกอีกทางหนึ่งที่ใช้สำหรับ สร้าง อ่าน เอกสาร Excel (XLS, XLSX, CSV) จากการพัฒนาโดยใช้   C# หรือ VB.NET แต่บทความนี้จะขอยกตัวอย่างการพัฒนาเพื่อสร้างเอกสารโดยใช้ C# ค่ะ

    Step 1 

    • เปิด Visual Studio
    • File>New>Project
    • เลือก Visual C#> Windows > Select Console Application

    Step

    • เลือก >> Project Menu >> เลือก Add References >> เลือกที่ Tab COM >> กด “Microsoft Excel 12.0 Object Library”  เพื่อเพิ่ม References
    รูปตัวอย่างการ Add References

    Step

    ตัวอย่าง Code C# เพื่อใช้ในการสร้าง Excel File ดังนี้

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using Microsoft.Office.Interop;
    using Microsoft.Office.Interop.Excel;
     
    namespace ConsoleApplication1
    {
        class Class1
        {
            public static void Main(string[] ar)
            {
                Application ExcelApp = new Application();
                Workbook ExcelWorkBook = null;
                Worksheet ExcelWorkSheet = null;
     
                ExcelApp.Visible = true;
                ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                // ExcelWorkBook.Worksheets.Add(); //Adding New Sheet in Excel Workbook
                try
                {
                    ExcelWorkSheet = ExcelWorkBook.Worksheets[1]; // Compulsory Line in which sheet you want to write data
                    //Writing data into excel of 100 rows with 10 column 
                    for (int r = 1; r < 101; r++) //r stands for ExcelRow and c for ExcelColumn
                    {
                        // Excel row and column start positions for writing Row=1 and Col=1
                        for (int c = 1; c < 11; c++)
                            ExcelWorkSheet.Cells[r, c] = "R" + r + "C" + c;
                    }
                    ExcelWorkBook.Worksheets[1].Name = "MySheet";//Renaming the Sheet1 to MySheet
                    ExcelWorkBook.SaveAs("d:\\Testing.xlsx");
                    ExcelWorkBook.Close();
                    ExcelApp.Quit();
                    Marshal.ReleaseComObject(ExcelWorkSheet);
                    Marshal.ReleaseComObject(ExcelWorkBook);
                    Marshal.ReleaseComObject(ExcelApp);
                }
                catch (Exception exHandle)
                {
                    Console.WriteLine("Exception: " + exHandle.Message);
                    Console.ReadLine();
                }
                finally
                {
     
                    foreach (Process process in Process.GetProcessesByName("Excel"))
                        process.Kill();
                }
             }
        }
    }
    

    เมื่อมีการ Run โปรแกรมแล้ว Excel file ก็จะถูกสร้างตาม path ที่ระบุ ค่ะ

     แหล่งอ้างอิง https://www.c-sharpcorner.com/blogs/creating-excel-file-using-interop-services1