重要代码:
1 row = new DOS.Row() 2 { 3 RowIndex = new DOX.UInt32Value(rowIndex), 4 Spans = new DOX.ListValue<DOX.StringValue>(new DOX.ListValue<DOX.StringValue>() 5 { 6 InnerText = "1:2" 7 }), 8 DyDescent = new DOX.DoubleValue(0.2), 9 OutlineLevel = new DOX.ByteValue().Value = 1 10 11 };
注意:如果多个行组合时,各组合之间的行不能连续,否则认为是一个行组合。如组合一的行为123,组合二的行为456,则认为是一个行组合,行为123456
完整代码:
1 /// <summary> 2 /// 行组 3 /// </summary> 4 /// <param name="fileName"></param> 5 /// <param name="sheetName"></param> 6 public static void CreateRowGroup(string fileName,string sheetName) 7 { 8 using(pkg.SpreadsheetDocument document=pkg.SpreadsheetDocument.Create(fileName, DOX.SpreadsheetDocumentType.Workbook)) 9 { 10 pkg.WorkbookPart workbookPart = document.AddWorkbookPart(); 11 DOS.Workbook workbook = workbookPart.Workbook = new DOS.Workbook(); 12 13 pkg.SharedStringTablePart sharedStringTablePart = workbookPart.AddNewPart<pkg.SharedStringTablePart>(); 14 15 pkg.WorksheetPart worksheetPart = workbookPart.AddNewPart<pkg.WorksheetPart>(); 16 DOS.SheetData sheetData = new DOS.SheetData(); 17 18 DOS.Worksheet worksheet = worksheetPart.Worksheet = new DOS.Worksheet(sheetData); 19 20 DOS.Sheets sheets = document.WorkbookPart.Workbook.AppendChild<DOS.Sheets>(new DOS.Sheets()); 21 22 DOS.Sheet sheet = new DOS.Sheet() 23 { 24 Id = workbookPart.GetIdOfPart(worksheetPart), 25 SheetId = 1, 26 Name = sheetName 27 }; 28 sheets.Append(sheet); 29 30 DOS.Row row; 31 for(uint rowIndex=1;rowIndex<100;rowIndex++) 32 { 33 if(rowIndex % 5 == 0) 34 { 35 row = new DOS.Row() 36 { 37 RowIndex = new DOX.UInt32Value(rowIndex) 38 }; 39 } 40 else 41 { 42 row = new DOS.Row() 43 { 44 RowIndex = new DOX.UInt32Value(rowIndex), 45 Spans = new DOX.ListValue<DOX.StringValue>(new DOX.ListValue<DOX.StringValue>() 46 { 47 InnerText = "1:2" 48 }), 49 DyDescent = new DOX.DoubleValue(0.2), 50 OutlineLevel = new DOX.ByteValue().Value = 1 51 52 }; 53 } 54 55 sheetData.Append(row); 56 57 Random random = new Random((int)DateTime.Now.Ticks* (int)rowIndex); 58 string text = random.Next(1,2000).ToString(); 59 60 int textIndex = InsertSharedStringItem(text, sharedStringTablePart); 61 62 DOS.Cell cell = new DOS.Cell() 63 { 64 CellReference = "A" + rowIndex.ToString(), 65 CellValue = new DOS.CellValue(textIndex.ToString()) 66 }; 67 cell.DataType = new DOX.EnumValue<DOS.CellValues>(DOS.CellValues.SharedString); 68 69 row.AppendChild(cell); 70 71 random = new Random((int)DateTime.Now.Ticks * (int)rowIndex); 72 text = random.Next(10000, 2000000).ToString(); 73 74 textIndex = InsertSharedStringItem(text, sharedStringTablePart); 75 76 cell = new DOS.Cell() 77 { 78 CellReference = "B" + rowIndex.ToString(), 79 CellValue = new DOS.CellValue(textIndex.ToString()) 80 }; 81 cell.DataType = new DOX.EnumValue<DOS.CellValues>(DOS.CellValues.SharedString); 82 83 row.AppendChild(cell); 84 } 85 86 worksheet.Save(); 87 //workbook.Save(); 88 //document.Save(); 89 90 } 91 }
运行后的结果图:
分享和发布原创的新技术,中国的软件行业落后外国几百条街了,知识共享力争不要落后太多