1. Táirgí
  2.   Scarbhileog
  3.   .NET
  4.   ClosedXML
 
  

Leabharlann Foinse Oscailte .NET chun Scarbhileoga Excel a Ionramháil

Ligeann ClosedXML duit Doiciméid Microsoft Excel a léamh, a láimhseáil agus a scríobh

Is C# API foinse oscailte é ClosedXML chun doiciméid Microsoft Excel 2007+ (.xlsx, .xlsm) a léamh, a láimhseáil agus a scríobh. Ligeann an API duit comhaid Excel a chruthú gan feidhmchlár Excel a úsáid, agus comhaid a léamh ag baint úsáide as na gnéithe feabhsaithe.

Ag baint úsáide as an API is féidir leat do leabhair oibre a stíliú trí úsáid a bhaint as dathú cúlra agus teorannú cille. Is féidir leat do bhileoga oibre a chur leis, a bhaint agus a bhogadh agus cineálacha sonraí a bhainistiú taobh istigh de Excel.

Previous Next

Tús a chur le ClosedXML

Is ó NuGet an bealach molta chun ClosedXML a shuiteáil, Bain úsáid as an ordú seo a leanas le haghaidh suiteáil níos tapúla.

Suiteáil ClosedXML ó NuGet

 Install-Package ClosedXML

Cruthaigh Leabhair Oibre Nua Saor in Aisce le C #

Ligeann CLosedXML d’fhorbróirí C# .NET bileoga oibre nua barr feabhais a chruthú. Is féidir leat leabhar oibre bán a chruthú leis an modh XLWorkbook(). Tá roinnt gnéithe tábhachtacha curtha san áireamh sa leabharlann chun do dhoiciméid bileoga oibre a bhainistiú. Ligeann sé bileoga oibre a chur i do leabhar oibre, cuir cealla agus colúin leis, cuir stíleanna i bhfeidhm ar sraitheanna agus colúin, scrios na cealla nach dteastaíonn agus go leor eile.

Cruthaigh Leabhair Oibre Nua trí .NET API


using (var workbook = new XLWorkbook())
{
    var worksheet = workbook.Worksheets.Add("Sample Sheet");
    worksheet.Cell("A1").Value = "Hello World!";
    worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)";
    workbook.SaveAs("HelloWorld.xlsx");
}

Stílbhileoga Excel ag úsáid C#

Cuireann an API foinse oscailte ClosedXML ar chumas forbróirí bogearraí stíleanna a chur i bhfeidhm ar a mbileoga Excel gan ach cúpla líne de chód C#. Tá an API saibhrithe le gnéithe stílithe lena n-áirítear ailíniú, teorainn, líonadh, cló, formáid uimhreacha, sraitheanna stílithe, colúin, agus go leor eile. Ceadaíonn an leabharlann freisin d'úsáideoirí an stíliú a athrú de réir a gcuid riachtanas.

Conas Stíleanna a chur i bhFeidhm ar Bhileoga Excel trí Leabharlann C#


            var path = Program.BaseCreatedDirectory;
            new StyleFont().Create(Path.Combine(path, "styleFont.xlsx"));
            new StyleFill().Create(Path.Combine(path, "styleFill.xlsx"));
            new StyleBorder().Create(Path.Combine(path, "styleBorder.xlsx"));
            new StyleAlignment().Create(Path.Combine(path, "styleAlignment.xlsx"));
            new StyleNumberFormat().Create(Path.Combine(path, "styleNumberFormat.xlsx"));
            new StyleIncludeQuotePrefix().Create(Path.Combine(path, "styleIncludeQuotePrefix.xlsx"));

Úsáid Scagairí Uathoibríoch in Excel ag úsáid C#

Tá tacaíocht iomlán san áireamh sa leabharlann ClosedXML chun scagairí a chur i bhfeidhm laistigh de do bhileoga oibre Excel. Tá cineálacha éagsúla scagairí curtha san áireamh sa leabharlann ar mhaithe le caoithiúlacht an úsáideora. Is féidir leat an scagaire a chur i bhfeidhm ar raon sonrach, scagaire a chur i bhfeidhm ar luachanna agus do scagairí saincheaptha féin a chruthú freisin.

Conas Scagairí Uathoibríocha a Chur i bhFeidhm in Excel trí C #

public class DynamicAutoFilter : IXLExample
    {
        public void Create(string filePath)
        {
            var wb = new XLWorkbook();
            IXLWorksheet ws;
            #region Single Column Numbers
            String singleColumnNumbers = "Single Column Numbers";
            ws = wb.Worksheets.Add(singleColumnNumbers);
            // Add a bunch of numbers to filter
            ws.Cell("A1").SetValue("Numbers")
                         .CellBelow().SetValue(2)
                         .CellBelow().SetValue(3)
                         .CellBelow().SetValue(3)
                         .CellBelow().SetValue(5)
                         .CellBelow().SetValue(1)
                         .CellBelow().SetValue(4);
            // Add filters
            ws.RangeUsed().SetAutoFilter().Column(1).AboveAverage();
            // Sort the filtered list
            //ws.AutoFilter.Sort(1);
            #endregion
            #region Multi Column
            String multiColumn = "Multi Column";
            ws = wb.Worksheets.Add(multiColumn);
            ws.Cell("A1").SetValue("First")
             .CellBelow().SetValue("B")
             .CellBelow().SetValue("C")
             .CellBelow().SetValue("C")
             .CellBelow().SetValue("E")
             .CellBelow().SetValue("A")
             .CellBelow().SetValue("D");
            ws.Cell("B1").SetValue("Numbers")
                         .CellBelow().SetValue(2)
                         .CellBelow().SetValue(3)
                         .CellBelow().SetValue(3)
                         .CellBelow().SetValue(5)
                         .CellBelow().SetValue(1)
                         .CellBelow().SetValue(4);
            ws.Cell("C1").SetValue("Strings")
             .CellBelow().SetValue("B")
             .CellBelow().SetValue("C")
             .CellBelow().SetValue("C")
             .CellBelow().SetValue("E")
             .CellBelow().SetValue("A")
             .CellBelow().SetValue("D");
            // Add filters
            ws.RangeUsed().SetAutoFilter().Column(2).BelowAverage();
            // Sort the filtered list
            //ws.AutoFilter.Sort(3);
            #endregion
            using (var ms = new MemoryStream())
            {
                wb.SaveAs(ms);
                var workbook = new XLWorkbook(ms);
                #region Single Column Numbers
                //workbook.Worksheet(singleColumnNumbers).AutoFilter.Sort(1, XLSortOrder.Descending);
                #endregion
                #region Multi Column 
                //workbook.Worksheet(multiColumn).AutoFilter.Sort(3, XLSortOrder.Descending);
                #endregion
                workbook.SaveAs(filePath);
                ms.Close();
            }
        }
    }
 Gaeilge