public static bool ExportToExcel(string filepath, DataSet ds, out string reason)
{
reason = string.Empty;
try
{
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
Worksheet ws = new Worksheet();
WorkbookStylesPart wbsp = workbookpart.AddNewPart<WorkbookStylesPart>();
// add styles to sheet
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();
for (int i = 0; i < ds.Tables.Count; i++)
{
Sheet sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = ds.Tables[i].TableName,
};
sheets.Append(sheet);
var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
Row rowCaption = new Row();
for (int col = 0; col < ds.Tables[i].Columns.Count - 1; col++)
{
Cell titleCell = new Cell();
//titleCell.StyleIndex = (UInt32Value)1U;
titleCell.CellValue = new CellValue(ds.Tables[i].Columns[col].Caption);
titleCell.DataType = new EnumValue<CellValues>(CellValues.String);
rowCaption.Append(titleCell);
}
sheetData.Append(rowCaption);
for (int row = 0; row < ds.Tables[i].Rows.Count; row++)
{
Row rowData = new Row();
string type = ds.Tables[i].Rows[row][6].ToString();
for (int col = 0; col < ds.Tables[i].Columns.Count - 1; col++)
{
Cell dataCell = new Cell();
if (type == "p")
dataCell.StyleIndex = 1;
else
dataCell.StyleIndex = 0;
var data = ds.Tables[i].Rows[row][col];
if (data is DateTime time)
{
dataCell.CellValue = new CellValue(time);
dataCell.DataType = new EnumValue<CellValues>(CellValues.String);
}
else if (data is double value)
{
dataCell.CellValue = new CellValue(data.ToString());
dataCell.DataType = new EnumValue<CellValues>(CellValues.Number);
}
else
{
dataCell.CellValue = new CellValue(data.ToString());
dataCell.DataType = new EnumValue<CellValues>(CellValues.String);
}
rowData.Append(dataCell);
}
sheetData.Append(rowData);
}
}
workbookpart.Workbook.Save();
spreadsheetDocument.Close();
}
catch (Exception ex)
{
LOG.Write(ex);
reason = ex.Message;
return false;
}
return true;
}
private static Stylesheet CreateStylesheet()
{
Stylesheet stylesheet1 = new Stylesheet() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "x14ac" } };
stylesheet1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
stylesheet1.AddNamespaceDeclaration("x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
Fills fills1 = new Fills() { Count = (UInt32Value)5U };
Fill fill1 = new Fill();
PatternFill patternFill1 = new PatternFill() { PatternType = PatternValues.None };
fill1.Append(patternFill1);
Fill fill2 = new Fill();
PatternFill patternFill2 = new PatternFill() { PatternType = PatternValues.Gray125 };
fill2.Append(patternFill2);
Fill fill3 = new Fill();
PatternFill patternFill5 = new PatternFill() { PatternType = PatternValues.Solid };
ForegroundColor foregroundColor3 = new ForegroundColor() { Rgb = "FFD9E1F3" };
BackgroundColor backgroundColor3 = new BackgroundColor() { Indexed = (UInt32Value)64U };
patternFill5.Append(foregroundColor3);
patternFill5.Append(backgroundColor3);
fill3.Append(patternFill5);
fills1.Append(fill1);
fills1.Append(fill2);
fills1.Append(fill3);
Borders borders1 = new Borders() { Count = (UInt32Value)1U };
DocumentFormat.OpenXml.Spreadsheet.Border border1 = new DocumentFormat.OpenXml.Spreadsheet.Border();
DocumentFormat.OpenXml.Drawing.LeftBorder leftBorder1 = new DocumentFormat.OpenXml.Drawing.LeftBorder();
DocumentFormat.OpenXml.Drawing.RightBorder rightBorder1 = new DocumentFormat.OpenXml.Drawing.RightBorder();
DocumentFormat.OpenXml.Drawing.TopBorder topBorder1 = new DocumentFormat.OpenXml.Drawing.TopBorder();
DocumentFormat.OpenXml.Drawing.BottomBorder bottomBorder1 = new DocumentFormat.OpenXml.Drawing.BottomBorder();
DiagonalBorder diagonalBorder1 = new DiagonalBorder();
border1.Append(leftBorder1);
border1.Append(rightBorder1);
border1.Append(topBorder1);
border1.Append(bottomBorder1);
border1.Append(diagonalBorder1);
borders1.Append(border1);
CellStyleFormats cellStyleFormats1 = new CellStyleFormats() { Count = (UInt32Value)1U };
CellFormat cellFormat1 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U };
cellStyleFormats1.Append(cellFormat1);
CellFormats cellFormats1 = new CellFormats() { Count = (UInt32Value)4U };
CellFormat cellFormat2 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U };
CellFormat cellFormat3 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyFill = true };
CellFormat cellFormat4 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)3U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyFill = true };
CellFormat cellFormat5 = new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)4U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyFill = true };
cellFormats1.Append(cellFormat2);
cellFormats1.Append(cellFormat3);
cellFormats1.Append(cellFormat4);
cellFormats1.Append(cellFormat5);
CellStyles cellStyles1 = new CellStyles() { Count = (UInt32Value)1U };
CellStyle cellStyle1 = new CellStyle() { Name = "Normal", FormatId = (UInt32Value)0U, BuiltinId = (UInt32Value)0U };
cellStyles1.Append(cellStyle1);
DifferentialFormats differentialFormats1 = new DifferentialFormats() { Count = (UInt32Value)0U };
TableStyles tableStyles1 = new TableStyles() { Count = (UInt32Value)0U, DefaultTableStyle = "TableStyleMedium2", DefaultPivotStyle = "PivotStyleMedium9" };
stylesheet1.Append(fills1);
stylesheet1.Append(borders1);
stylesheet1.Append(cellStyleFormats1);
stylesheet1.Append(cellFormats1);
stylesheet1.Append(cellStyles1);
stylesheet1.Append(differentialFormats1);
stylesheet1.Append(tableStyles1);
return stylesheet1;
}