You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
4.3 KiB
120 lines
4.3 KiB
using DocumentFormat.OpenXml.Packaging;
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ExcelHelperFW
|
|
{
|
|
public class ExcelHelper
|
|
{
|
|
private static readonly ExcelHelper _instance = new ExcelHelper();
|
|
|
|
public static ExcelHelper Instance { get { return _instance; } }
|
|
|
|
/// <summary>
|
|
/// Get excel data
|
|
/// </summary>
|
|
/// <param name="sourcePath">excel path</param>
|
|
/// <param name="sheetName">sheet name</param>
|
|
/// <param name="rowStartPosition">row start position (first row = 1)</param>
|
|
/// <param name="tempPath">copy path</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public List<Dictionary<string, string>> GetData(string sourcePath, string sheetName, bool removeHeader, int rowStartPosition = 1, string tempPath = "./temp_file.xlsx")
|
|
{
|
|
File.Copy(sourcePath, tempPath, true);
|
|
|
|
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
using (SpreadsheetDocument doc = SpreadsheetDocument.Open(tempPath, true))
|
|
{
|
|
WorkbookPart workbookPart = doc.WorkbookPart;
|
|
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName);
|
|
|
|
if (sheet == null)
|
|
throw new Exception("Cannot find sheet.");
|
|
|
|
WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
|
|
Worksheet worksheet = worksheetPart.Worksheet;
|
|
|
|
List<Row> rows = worksheet.Descendants<Row>().Where(r => r.RowIndex >= rowStartPosition).ToList();
|
|
if (removeHeader)
|
|
rows.RemoveAt(0);
|
|
|
|
for (int i = 0; i < rows.Count; i++)
|
|
{
|
|
Dictionary<string, string> rowPairs = RowToKeyValuePairs(rows[i], workbookPart);
|
|
if (rowPairs == null || rowPairs.Count < 1)
|
|
continue;
|
|
|
|
data.Add(rowPairs);
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private Dictionary<string, string> RowToKeyValuePairs(Row row, WorkbookPart workbookPart)
|
|
{
|
|
Dictionary<string, string> pairs = new Dictionary<string, string>();
|
|
var cells = row.Elements<Cell>().ToList();
|
|
|
|
for (int col = 0; col < cells.Count; col++)
|
|
{
|
|
Cell cell = cells[col];
|
|
|
|
string cellReference = cell.CellReference.Value;
|
|
string columnName = Regex.Replace(cellReference, @"\d", "");
|
|
//int rowNumber = int.Parse(Regex.Replace(cellReference, "[^0-9]", ""));
|
|
|
|
string cellValue = cell.CellValue?.InnerText;
|
|
if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
|
|
{
|
|
int.TryParse(cellValue, out int sharedStringId);
|
|
cellValue = workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(sharedStringId).InnerText;
|
|
}
|
|
|
|
pairs.Add(columnName, cellValue);
|
|
}
|
|
|
|
return pairs;
|
|
}
|
|
|
|
private string ConvertValue(Dictionary<string, string> pairs, string key)
|
|
{
|
|
if (!pairs.ContainsKey(key))
|
|
return null;
|
|
|
|
return pairs[key];
|
|
}
|
|
|
|
private T ConvertValue<T>(Dictionary<string, string> pairs, string key, Func<string, T> func)
|
|
{
|
|
if (!pairs.ContainsKey(key) || func == null)
|
|
return default(T);
|
|
|
|
string value = pairs[key];
|
|
return func(value);
|
|
}
|
|
|
|
private DateTime? ConvertValueToDateTime(Dictionary<string, string> pairs, string key)
|
|
{
|
|
return ConvertValue<DateTime?>(pairs, key, x =>
|
|
{
|
|
if (string.IsNullOrEmpty(x))
|
|
return null;
|
|
|
|
if (double.TryParse(x, out double d))
|
|
return DateTime.FromOADate(d);
|
|
else
|
|
return null;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|