usingDocumentFormat.OpenXml;usingDocumentFormat.OpenXml.Packaging;usingDocumentFormat.OpenXml.Spreadsheet;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text.RegularExpressions;namespaceOpenXMLSDK_Sample7{classProgram{staticvoidMain(string[]args){conststringFILE=@"C:\Users\Public\Documents\openxmlsdk14.xlsx";conststringSHEET_NAME="Sheet1";using(SpreadsheetDocumentexcelDoc=SpreadsheetDocument.Open(FILE,true)){WorkbookPartbookPart=excelDoc.WorkbookPart;intsheet_index=GetWorksheetIndex(bookPart,SHEET_NAME);WorksheetPartsheet_part=GetWorksheetPart(bookPart,sheet_index);//A1Cellcell=GetCell(sheet_part,"A1");Fontfont=newFont(){Color=newColor(){Rgb=newHexBinaryValue("FF00FF00")},Bold=newBold(){Val=newBooleanValue(true)}};SetFont(bookPart,cell,SHEET_NAME,font);//A2cell=GetCell(sheet_part,"A2");font=newFont(){Color=newColor(){Indexed=newUInt32Value((uint)4)},FontName=newFontName(){Val="Times New Roman"},FontSize=newFontSize(){Val=newDoubleValue(14.0)},Strike=newStrike(){Val=newBooleanValue(true)}};SetFont(bookPart,cell,SHEET_NAME,font);}//ファイルを開くSystem.Diagnostics.Process.Start(FILE);}privatestaticvoidSetFont(WorkbookPartbookPart,Cellcell,stringsheet_name,Fontfont_to_set){if(cell==null||bookPart==null){return;}IEnumerable<CellFormat>cell_formats=bookPart.WorkbookStylesPart.Stylesheet.CellFormats.Elements<CellFormat>();CellFormatformat_to;if(cell.StyleIndex!=null){CellFormatcell_xf=cell_formats.ElementAt(Convert.ToInt32(cell.StyleIndex.Value));intsheet_index=GetWorksheetIndex(bookPart,sheet_name);WorksheetPartsheet_part=GetWorksheetPart(bookPart,sheet_index);intreferenced_count=sheet_part.Worksheet.Descendants<Cell>().Count(c=>c.StyleIndex==cell.StyleIndex);if(referenced_count==0){format_to=cell_xf;}else{format_to=cell_xf.Clone()asCellFormat;bookPart.WorkbookStylesPart.Stylesheet.CellFormats.Append(format_to);cell.StyleIndex=newUInt32Value((uint)cell_formats.Count()-1);}}else{format_to=newCellFormat();format_to.Alignment=newAlignment(){Vertical=newEnumValue<VerticalAlignmentValues>(VerticalAlignmentValues.Center)};bookPart.WorkbookStylesPart.Stylesheet.CellFormats.Append(format_to);cell.StyleIndex=newUInt32Value((uint)cell_formats.Count()-1);}//Get the x:font to modifyFontfont=null;Fontsfonts=bookPart.WorkbookStylesPart.Stylesheet.Fonts;if(format_to.FontId!=null&&format_to.FontId.HasValue){font=fonts.Elements<Font>().ElementAt(Convert.ToInt32(format_to.FontId.Value));}else{font=newFont();font.FontSize=newFontSize(){Val=11};fonts.Append(font);fonts.Count++;format_to.FontId=newUInt32Value((uint)fonts.Count()-1);}//Copy the font_to_set to the font.CopyFont(font_to_set,font);bookPart.WorkbookStylesPart.Stylesheet.Save();}privatestaticvoidCopyFont(Fontfont_from,Fontfont_to){if(font_to.FontSize!=null){font_to.FontSize=newFontSize(){Val=font_to.FontSize.Val};}if(font_from.Color!=null){if(font_to.Color==null){font_to.Color=newColor();}if(font_from.Color.Indexed!=null){font_to.Color.Indexed=newUInt32Value(font_from.Color.Indexed.Value);}if(font_from.Color.Rgb!=null){font_to.Color.Rgb=newHexBinaryValue(font_from.Color.Rgb);}if(font_from.Color.Theme!=null){font_to.Color.Theme=newUInt32Value(font_from.Color.Theme.Value);}if(font_from.Color.Tint!=null){font_to.Color.Tint=newDoubleValue(font_from.Color.Tint.Value);}}if(font_from.FontName!=null){font_to.FontName=newFontName(){Val=font_from.FontName.Val};}if(font_from.Bold!=null){font_to.Bold=newBold(){Val=newBooleanValue(font_from.Bold.Val)};}if(font_from.Italic!=null){font_to.Italic=newItalic(){Val=newBooleanValue(font_from.Italic.Val)};}if(font_from.Underline!=null){font_to.Underline=newUnderline(){Val=font_from.Underline.Val};}if(font_from.Strike!=null){font_to.Strike=newStrike(){Val=newBooleanValue(font_from.Strike.Val)};}if(font_from.VerticalTextAlignment!=null){font_to.VerticalTextAlignment=newVerticalTextAlignment(){Val=font_from.VerticalTextAlignment.Val};}if(font_from.FontFamilyNumbering!=null){font_to.FontFamilyNumbering=newFontFamilyNumbering(){Val=font_from.FontFamilyNumbering.Val};}if(font_from.FontCharSet!=null){font_to.FontCharSet=newFontCharSet(){Val=font_from.FontCharSet.Val};}if(font_from.FontScheme!=null){font_to.FontScheme=newFontScheme(){Val=font_from.FontScheme.Val};}}// 以下省略...}}