重要代码:

  DOS.SheetData sheetData = new DOS.SheetData();

                 DOS.Columns columns = new DOS.Columns();
DOS.Column column = null;
uint min = ; for (int ind = ; ind <= ; ind += )
{
if (ind < )
continue;
column = columns.AppendChild<DOS.Column>(new DOS.Column()); if (ind - <= )
{
min = ;
}
else
{
min = (uint)ind - ;
}
column.Min = new DOX.UInt32Value(min);
uint max = (uint)ind - ;
if (max <= )
max = ;
column.Max = new DOX.UInt32Value(max);
column.OutlineLevel = ;
} DOS.Worksheet worksheet = worksheetPart.Worksheet = new DOS.Worksheet(columns,sheetData);

如果存在多个列组合,列组合之间的列不能连续,否则认为是一个列组合

完整代码:

        /// <summary>
/// 列组
/// </summary>
/// <param name="fileName"></param>
/// <param name="sheetName"></param>
public static void CreateColumnGroup(string fileName,string sheetName)
{
using (pkg.SpreadsheetDocument document = pkg.SpreadsheetDocument.Create(fileName, DOX.SpreadsheetDocumentType.Workbook))
{
pkg.WorkbookPart workbookPart = document.AddWorkbookPart();
DOS.Workbook workbook = workbookPart.Workbook = new DOS.Workbook(); pkg.SharedStringTablePart sharedStringTablePart = workbookPart.AddNewPart<pkg.SharedStringTablePart>(); pkg.WorksheetPart worksheetPart = workbookPart.AddNewPart<pkg.WorksheetPart>();
DOS.SheetData sheetData = new DOS.SheetData(); DOS.Columns columns = new DOS.Columns();
DOS.Column column = null;
uint min = ; for (int ind = ; ind <= ; ind += )
{
if (ind < )
continue;
column = columns.AppendChild<DOS.Column>(new DOS.Column()); if (ind - <= )
{
min = ;
}
else
{
min = (uint)ind - ;
}
column.Min = new DOX.UInt32Value(min);
uint max = (uint)ind - ;
if (max <= )
max = ;
column.Max = new DOX.UInt32Value(max);
column.OutlineLevel = ;
} DOS.Worksheet worksheet = worksheetPart.Worksheet = new DOS.Worksheet(columns,sheetData); DOS.Sheets sheets = document.WorkbookPart.Workbook.AppendChild<DOS.Sheets>(new DOS.Sheets()); DOS.Sheet sheet = new DOS.Sheet()
{
Id = workbookPart.GetIdOfPart(worksheetPart),
SheetId = ,
Name = sheetName
};
sheets.Append(sheet); DOS.Row row; for (uint rowIndex = ; rowIndex < ; rowIndex++)
{
row = new DOS.Row()
{
RowIndex = new DOX.UInt32Value(rowIndex)
}; sheetData.Append(row); Random random = null; DOS.Cell cell = null;
for(int colindex=;colindex<=;colindex++)
{
random = new Random((int)DateTime.Now.Millisecond * (int)rowIndex);
string text = random.Next(, ).ToString(); int textIndex = InsertSharedStringItem(text, sharedStringTablePart); cell = new DOS.Cell()
{
CellReference = colindex.ToExcelName() + rowIndex.ToString(),
CellValue = new DOS.CellValue(textIndex.ToString())
};
cell.DataType = new DOX.EnumValue<DOS.CellValues>(DOS.CellValues.SharedString);
row.AppendChild(cell);
} } worksheet.Save(); }
}
05-11 11:05