読み書きdocxと.xlsxドキュメントのC#クラス-Simple OOXML


Simple OOXMLはCodePlex上のオープンソースプロジェクトで、Open Xml SDK 2.0を使用して、一連の読み書きをカプセル化している.docxと.xlsxドキュメントのクラスは、作業を大幅に簡素化できます.
次のクラスがあります.
  • SpreadsheetReader - manipulation of templates, retrieval of document parts, row and column reference functionality
  • SpreadsheetWriter - writing of document parts and creation of document level attributes. Add or remove spreadsheets.
  • SpreadsheetStyle - encapsulates font, border and fill handing in a spreadsheet.
  • WoksheetReader - retrieves cell and style information from a worksheet
  • WorksheetWriter - allows the pasting or insertion of data and style - using simple value types or DataTables - at a cell or range reference.
  • DocumentReader - retrieval of document templates.
  • DocumentWriter - pastes and saves text and text lists using predefined bookmarks.

  • サンプルコード:
    using (var stream = SpreadsheetReader.StreamFromFile("test.xlsx"))
    {
        using (var doc = SpreadsheetDocument.Open(stream, true))
        {
            var sheet = SpreadsheetReader.GetWorksheetPartByName(doc, "Sheet1");
            var writer = new WorksheetWriter(doc, sheet);
     
            //      
            var style1 = SpreadsheetStyle.GetDefault(doc);
            style1.SetBackgroundColor("97DDFF");//      
            style1.SetHorizontalAlignment(HorizontalAlignmentValues.Center);//        
            style1.SetBorder("000000", BorderStyleValues.Thin);//    
            var data = new[] {
                new {Name = "  ",Sex = " ",Age = 20},
                new {Name = "  ",Sex = " ",Age = 19}
            };
     
            int rowIndex = 2;
            for (int i = 0; i < data.Length; i++)
            {
                writer.PasteText("A" + rowIndex, data[i].Name, style1);//    
                writer.PasteText("B" + rowIndex, data[i].Sex, style1);//    
                writer.PasteNumber("C" + rowIndex, data[i].Age.ToString(), style1);//    
                rowIndex++;
            }
            writer.Save();//          
            SpreadsheetWriter.StreamToFile("test.xlsx", stream);//      
        }
    }