| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.Data;
 
- using System.Linq;
 
- using System.Text;
 
- using System.Threading.Tasks;
 
- using Aitex.Core.RT.Log;
 
- using DocumentFormat.OpenXml;
 
- using DocumentFormat.OpenXml.Packaging;
 
- using DocumentFormat.OpenXml.Spreadsheet;
 
- namespace MECF.Framework.Common.Utilities
 
- {
 
-     public class ExcelHelper
 
-     {
 
-         public static bool ExportToExcel(string filepath, DataSet ds, out string reason, bool createNewFile = true, bool isNeedNumeric = true)
 
-         {
 
-             reason = string.Empty;
 
-             try
 
-             {
 
-                 SpreadsheetDocument spreadsheetDocument;
 
-                 WorkbookPart workbookpart;
 
-                 WorksheetPart worksheetPart;
 
-                 Sheets sheets;
 
-                 if (createNewFile)
 
-                 {
 
-                     spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
 
-                     workbookpart = spreadsheetDocument.AddWorkbookPart();
 
-                     workbookpart.Workbook = new Workbook();
 
-                     sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
 
-                 }
 
-                 else
 
-                 {
 
-                     spreadsheetDocument = SpreadsheetDocument.Open(filepath, true);
 
-                     workbookpart = spreadsheetDocument.WorkbookPart;
 
-                     sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
 
-                 }
 
-                 for (int i = 0; i < ds.Tables.Count; i++)
 
-                 {
 
-                     worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
 
-                     worksheetPart.Worksheet = new Worksheet(new SheetData());
 
-                     sheets = workbookpart.Workbook.GetFirstChild<Sheets>();
 
-                     uint sheetID = 1;
 
-                     if (sheets.Descendants<Sheet>().Count() > 0)
 
-                     {
 
-                         sheetID = sheets.Descendants<Sheet>().Select(S => S.SheetId.Value).Max() + 1;
 
-                     }
 
-                     Sheet sheet = new Sheet()
 
-                     {
 
-                         Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
 
-                         SheetId = sheetID,
 
-                         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; col++)
 
-                     {
 
-                         Cell titleCell = new Cell();
 
-                         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();
 
-                         for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
 
-                         {
 
-                             Cell dataCell = new Cell();
 
-                             var data = ds.Tables[i].Rows[row][col];
 
-                             bool isNumeric = false;
 
-                             if (isNeedNumeric)
 
-                             {
 
-                                 if (data != null)
 
-                                 {
 
-                                     if (double.TryParse(data.ToString(), out double doubleValue))
 
-                                     {
 
-                                         // 转换为 double 类型成功,表示为数字
 
-                                         isNumeric = true;
 
-                                     }
 
-                                     else if (int.TryParse(data.ToString(), out int intValue))
 
-                                     {
 
-                                         isNumeric = true;
 
-                                     }
 
-                                     else if (float.TryParse(data.ToString(), out float floatValue))
 
-                                     {
 
-                                         isNumeric = true;
 
-                                     }
 
-                                 }
 
-                             }
 
-                             if (isNumeric)
 
-                             {
 
-                                 // 处理数字类型的值
 
-                                 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);
 
-                             }
 
-                             //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.String);
 
-                             //}
 
-                             //else if (data is int intValue)
 
-                             //{
 
-                             //	dataCell.CellValue = new CellValue(intValue.ToString());
 
-                             //	dataCell.DataType = new EnumValue<CellValues>(CellValues.Number);
 
-                             //}
 
-                             //else if (data is float floatValue)
 
-                             //{
 
-                             //	dataCell.CellValue = new CellValue(floatValue.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;
 
-         }
 
-         public static bool ExportProcessLogToExcel(string filepath, DataSet ds, out string reason, bool createNewFile = true)
 
-         {
 
-             reason = string.Empty;
 
-             try
 
-             {
 
-                 SpreadsheetDocument spreadsheetDocument;
 
-                 WorkbookPart workbookpart;
 
-                 WorksheetPart worksheetPart;
 
-                 Sheets sheets;
 
-                 if (createNewFile)
 
-                 {
 
-                     spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
 
-                     workbookpart = spreadsheetDocument.AddWorkbookPart();
 
-                     workbookpart.Workbook = new Workbook();
 
-                     sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
 
-                 }
 
-                 else
 
-                 {
 
-                     spreadsheetDocument = SpreadsheetDocument.Open(filepath, true);
 
-                     workbookpart = spreadsheetDocument.WorkbookPart;
 
-                     workbookpart.Workbook = new Workbook();
 
-                     sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
 
-                 }
 
-                 for (int i = 0; i < ds.Tables.Count; i++)
 
-                 {
 
-                     worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
 
-                     worksheetPart.Worksheet = new Worksheet(new SheetData());
 
-                     sheets = workbookpart.Workbook.GetFirstChild<Sheets>();
 
-                     uint sheetID = 1;
 
-                     if (sheets.Descendants<Sheet>().Count() > 0)
 
-                     {
 
-                         sheetID = sheets.Descendants<Sheet>().Select(S => S.SheetId.Value).Max() + 1;
 
-                     }
 
-                     Sheet sheet = new Sheet()
 
-                     {
 
-                         Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
 
-                         SheetId = sheetID,
 
-                         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; col++)
 
-                     {
 
-                         Cell titleCell = new Cell();
 
-                         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();
 
-                         for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
 
-                         {
 
-                             Cell dataCell = new Cell();
 
-                             var data = ds.Tables[i].Rows[row][col];
 
-                             bool isNumeric = false;
 
-                             if (data != null)
 
-                             {
 
-                                 if (double.TryParse(data.ToString(), out double doubleValue))
 
-                                 {
 
-                                     // 转换为 double 类型成功,表示为数字
 
-                                     isNumeric = true;
 
-                                 }
 
-                                 else if (int.TryParse(data.ToString(), out int intValue))
 
-                                 {
 
-                                     isNumeric = true;
 
-                                 }
 
-                                 else if (float.TryParse(data.ToString(), out float floatValue))
 
-                                 {
 
-                                     isNumeric = true;
 
-                                 }
 
-                             }
 
-                             dataCell.CellValue = new CellValue(data.ToString());
 
-                             // 处理数字类型的值 // 处理非数字类型的值
 
-                             dataCell.DataType = isNumeric ? new EnumValue<CellValues>(CellValues.Number) : 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;
 
-         }
 
-         public static bool ExportProcessLogToExcel(string filepath, DataSet ds, int fristRow, int endRow, out string reason, bool createNewFile = true)
 
-         {
 
-             reason = string.Empty;
 
-             try
 
-             {
 
-                 SpreadsheetDocument spreadsheetDocument;
 
-                 WorkbookPart workbookpart;
 
-                 WorksheetPart worksheetPart;
 
-                 Sheets sheets;
 
-                 if (createNewFile)
 
-                 {
 
-                     spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
 
-                     workbookpart = spreadsheetDocument.AddWorkbookPart();
 
-                     workbookpart.Workbook = new Workbook();
 
-                     sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
 
-                 }
 
-                 else
 
-                 {
 
-                     spreadsheetDocument = SpreadsheetDocument.Open(filepath, true);
 
-                     workbookpart = spreadsheetDocument.WorkbookPart;
 
-                     workbookpart.Workbook = new Workbook();
 
-                     sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;
 
-                 }
 
-                 for (int i = 0; i < ds.Tables.Count; i++)
 
-                 {
 
-                     worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
 
-                     worksheetPart.Worksheet = new Worksheet(new SheetData());
 
-                     sheets = workbookpart.Workbook.GetFirstChild<Sheets>();
 
-                     uint sheetID = 1;
 
-                     if (sheets.Descendants<Sheet>().Count() > 0)
 
-                     {
 
-                         sheetID = sheets.Descendants<Sheet>().Select(S => S.SheetId.Value).Max() + 1;
 
-                     }
 
-                     Sheet sheet = new Sheet()
 
-                     {
 
-                         Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
 
-                         SheetId = sheetID,
 
-                         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; col++)
 
-                     {
 
-                         Cell titleCell = new Cell();
 
-                         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 = fristRow; row < endRow; row++)
 
-                     {
 
-                         Row rowData = new Row();
 
-                         for (int col = 0; col < ds.Tables[i].Columns.Count; col++)
 
-                         {
 
-                             Cell dataCell = new Cell();
 
-                             var data = ds.Tables[i].Rows[row][col];
 
-                             bool isNumeric = false;
 
-                             if (data != null)
 
-                             {
 
-                                 if (double.TryParse(data.ToString(), out double doubleValue))
 
-                                 {
 
-                                     // 转换为 double 类型成功,表示为数字
 
-                                     isNumeric = true;
 
-                                 }
 
-                                 else if (int.TryParse(data.ToString(), out int intValue))
 
-                                 {
 
-                                     isNumeric = true;
 
-                                 }
 
-                                 else if (float.TryParse(data.ToString(), out float floatValue))
 
-                                 {
 
-                                     isNumeric = true;
 
-                                 }
 
-                             }
 
-                             dataCell.CellValue = new CellValue(data.ToString());
 
-                             // 处理数字类型的值 // 处理非数字类型的值
 
-                             dataCell.DataType = isNumeric ? new EnumValue<CellValues>(CellValues.Number) : 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;
 
-         }
 
-     }
 
- }
 
 
  |