1. Produk
  2.   Hamparan
  3.   .NET
  4.   NetOffice
 
  

Pustaka .NET Percuma untuk Mencipta Dokumen Hamparan

Baca, Tulis, Manipulasi & Tukar fail Excel melalui Open Source .NET API.

NetOffice API membenarkan pembangun .NET untuk Membaca, Menulis, Memanipulasi & Menukar fail Excel melalui sumber terbuka .NET API. API membenarkan mengautomasikan Hamparan Microsoft Excel dan membangunkan Tambahan Microsoft Excel. Menggunakan API, pembangun akan menggunakan semua pilihan yang disertakan dalam MS Office versi 2000, 2002, 2003, 2007, 2010, 2013 dan 2016. API bergantung pada COM-architecture di mana anda mendapatkan semula objek proksi COM dalam aplikasi anda.

Untuk bekerja dengan dokumen Microsoft Excel, anda memerlukan ExcelApi.dll dengan OfficeApi.ddl, VBIDEApi.dll dan NetOffice.dll sebagai kebergantungan. Semua aplikasi Office menggunakan jenis yang ditakrifkan dalam perpustakaan komponen/jenis lain. Oleh itu, perpustakaan jenis bergantung ini diberikan sebagai perhimpunan bebas. Setiap pemasangan juga memerlukan pemasangan NetOffice.dll.

Previous Next

Bermula dengan NetOffice

Pertama sekali, anda perlu mempunyai .NET Framework 4.5 atau lebih tinggi. Selepas itu, sila muat turun repositori secara manual daripada GitHub atau pasangnya daripada NuGet.

Pemasangan  NetOffice daripada NuGet

 Install-Package NetOfficeFw.Excel

Tambah Bentuk dalam Excel menggunakan API C# Percuma

NetOffice membenarkan pengaturcara .NET menambah bentuk dalam Hamparan Microsoft Excel secara pengaturcaraan. Untuk menambah bentuk dalam fail excel terlebih dahulu, anda perlu memulakan Excel.Application dan mematikan kotak mesej. Selepas aplikasi excel anda dimulakan, anda boleh menambah dokumen baharu padanya menggunakan kaedah ExcelApplication.Workbooks.Add(). Anda boleh memasukkan teks dalam fail excel yang baru anda buat menggunakan workSheet.Cells[1, 1].Harta nilai dan tambah bentuk dalam fail menggunakan kaedah WorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShape32pointStar, 10, 50, 200, 20).

Masukkan Shapes ke Fail Hamparan Excel melalui C#

    // start excel and turn off msg boxes
    Excel.Application excelApplication = new Excel.Application();
    excelApplication.DisplayAlerts = false;
    // create a utils instance, not need for but helpful to keep the lines of code low
    CommonUtils utils = new CommonUtils(excelApplication);
    // add a new workbook
    Excel.Workbook workBook = excelApplication.Workbooks.Add();
    Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];
    workSheet.Cells[1, 1].Value = "NetOffice Excel Example 04";
    // create a star
    Excel.Shape starShape = workSheet.Shapes.AddShape(MsoAutoShapeType.msoShape32pointStar, 10, 50, 200, 20);
    // create a simple textbox
    Excel.Shape textBox = workSheet.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 10, 150, 200, 50);
    textBox.TextFrame.Characters().Text = "text";
    textBox.TextFrame.Characters().Font.Size = 14;
    // create a wordart
    Excel.Shape textEffect = workSheet.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect14, "WordArt", "Arial", 12,
                                                                                MsoTriState.msoTrue, MsoTriState.msoFalse, 10, 250);
    // save the book 
    string workbookFile = utils.File.Combine(HostApplication.RootDirectory, "Example04", DocumentFormat.Normal);
    workBook.SaveAs(workbookFile);
    // close excel and dispose reference
    excelApplication.Quit();
    excelApplication.Dispose();
    / show end dialog
    HostApplication.ShowFinishDialog(null, workbookFile);

Buat Carta dalam Excel menggunakan C#

NetOffice membenarkan pengaturcara .NET menambah carta dalam Fail Microsoft Excel secara pengaturcaraan. Untuk menambah carta dalam fail Excel; pertama, anda perlu memulakan Excel.Application dan matikan kotak mesej dan tambah lembaran kerja baharu menggunakan kaedah xcelApplication.Workbooks.Add(). Anda boleh memasukkan carta dalam fail excel anda yang baru dibuat dengan memulakan Excel.ChartObject dan menetapkannya menggunakan ((Excel.ChartObjects)workSheet.ChartObjects()).Add(70, 100, 375, 225) kaedah Anda boleh menetapkan sumber data bagi carta anda yang baru dibuat menggunakan kaedah Chart.SetSourceData().

Tambahkan Carta pada Lembaran Kerja Excel melalui C# API

    // start excel and turn off msg boxes
    Excel.Application excelApplication = new Excel.Application();
    excelApplication.DisplayAlerts = false;
    // create a utils instance, no need for but helpful to keep the lines of code low
    CommonUtils utils = new CommonUtils(excelApplication);
    // add a new workbook
    Excel.Workbook workBook = excelApplication.Workbooks.Add();
    Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];
    // we need some data to display
    Excel.Range dataRange = PutSampleData(workSheet);
    // create a nice diagram
    Excel.ChartObject chart = ((Excel.ChartObjects)workSheet.ChartObjects()).Add(70, 100, 375, 225);
    chart.Chart.SetSourceData(dataRange);
    // save the book 
    string workbookFile = utils.File.Combine(HostApplication.RootDirectory, "Example05", DocumentFormat.Normal);
    workBook.SaveAs(workbookFile);
    // close excel and dispose reference
    excelApplication.Quit();
    excelApplication.Dispose();
    // show end dialog
    HostApplication.ShowFinishDialog(null, workbookFile);
 Melayu