Skip to content

Commit 5a7a6f2

Browse files
authored
Merge pull request #1229 from iceljc/master
Parse csv file
2 parents a42b058 + 3e01c54 commit 5a7a6f2

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

BotSharp.sln

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ EndProject
150150
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.FuzzySharp", "src\Plugins\BotSharp.Plugin.FuzzySharp\BotSharp.Plugin.FuzzySharp.csproj", "{E7C243B9-E751-B3B4-8F16-95C76CA90D31}"
151151
EndProject
152152
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.MMPEmbedding", "src\Plugins\BotSharp.Plugin.MMPEmbedding\BotSharp.Plugin.MMPEmbedding.csproj", "{394B858B-9C26-B977-A2DA-8CC7BE5914CB}"
153+
EndProject
153154
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.Membase", "src\Plugins\BotSharp.Plugin.Membase\BotSharp.Plugin.Membase.csproj", "{13223C71-9EAC-9835-28ED-5A4833E6F915}"
154155
EndProject
155156
Global
@@ -720,7 +721,7 @@ Global
720721
{FC63C875-E880-D8BB-B8B5-978AB7B62983} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
721722
{242F2D93-FCCE-4982-8075-F3052ECCA92C} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
722723
{E7C243B9-E751-B3B4-8F16-95C76CA90D31} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
723-
{394B858B-9C26-B977-A2DA-8CC7BE5914CB} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
724+
{394B858B-9C26-B977-A2DA-8CC7BE5914CB} = {4F346DCE-087F-4368-AF88-EE9C720D0E69}
724725
{13223C71-9EAC-9835-28ED-5A4833E6F915} = {53E7CD86-0D19-40D9-A0FA-AB4613837E89}
725726
EndGlobalSection
726727
GlobalSection(ExtensibilityGlobals) = postSolution

src/Plugins/BotSharp.Plugin.ExcelHandler/Functions/ReadExcelFn.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using NPOI.HSSF.UserModel;
12
using NPOI.SS.UserModel;
23
using NPOI.XSSF.UserModel;
4+
using System.Globalization;
35
using System.Linq.Dynamic.Core;
46

57
namespace BotSharp.Plugin.ExcelHandler.Functions;
@@ -69,7 +71,7 @@ private void Init()
6971
{
7072
if (_excelFileTypes.IsNullOrEmpty())
7173
{
72-
_excelFileTypes = FileUtility.GetMimeFileTypes(["excel", "spreadsheet"]).ToHashSet();
74+
_excelFileTypes = FileUtility.GetMimeFileTypes(["excel", "spreadsheet", "csv"]).ToHashSet();
7375
}
7476
}
7577

@@ -133,7 +135,7 @@ private List<SqlContextOut> GetResponeFromDialogs(List<RoleDialogModel> dialogs)
133135
}
134136

135137
var binary = _fileStorage.GetFileBytes(file.FileStorageUrl);
136-
var workbook = ConvertToWorkBook(binary);
138+
var workbook = ConvertToWorkbook(binary, extension);
137139

138140
var currentCommands = _dbService.WriteExcelDataToDB(workbook);
139141
sqlCommands.AddRange(currentCommands);
@@ -167,11 +169,22 @@ private string GenerateSqlExecutionSummary(List<SqlContextOut> results)
167169
return stringBuilder.ToString();
168170
}
169171

170-
private IWorkbook ConvertToWorkBook(BinaryData binary)
172+
private IWorkbook ConvertToWorkbook(BinaryData binary, string extension)
171173
{
172-
using var fileStream = new MemoryStream(binary.ToArray());
173-
IWorkbook workbook = new XSSFWorkbook(fileStream);
174-
return workbook;
174+
var bytes = binary.ToArray();
175+
176+
if (extension.IsEqualTo(".csv"))
177+
{
178+
return ExcelHelper.ConvertCsvToWorkbook(bytes);
179+
}
180+
181+
using var fileStream = new MemoryStream(bytes);
182+
if (extension.IsEqualTo(".xls"))
183+
{
184+
return new HSSFWorkbook(fileStream);
185+
}
186+
187+
return new XSSFWorkbook(fileStream);
175188
}
176189
#endregion
177190
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using NPOI.SS.UserModel;
2+
using NPOI.XSSF.UserModel;
3+
using System.Globalization;
4+
5+
namespace BotSharp.Plugin.ExcelHandler.Helpers;
6+
7+
internal static class ExcelHelper
8+
{
9+
internal static IWorkbook ConvertCsvToWorkbook(byte[] bytes)
10+
{
11+
IWorkbook workbook = new XSSFWorkbook();
12+
ISheet sheet = workbook.CreateSheet("Sheet1");
13+
14+
using var memoryStream = new MemoryStream(bytes);
15+
using var reader = new StreamReader(memoryStream);
16+
17+
int rowIndex = 0;
18+
string? line;
19+
20+
while ((line = reader.ReadLine()) != null)
21+
{
22+
IRow row = sheet.CreateRow(rowIndex);
23+
var values = ParseCsvLine(line);
24+
25+
for (int colIndex = 0; colIndex < values.Count; colIndex++)
26+
{
27+
ICell cell = row.CreateCell(colIndex);
28+
var value = values[colIndex];
29+
30+
if (rowIndex > 0 && double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double numericValue))
31+
{
32+
cell.SetCellValue(numericValue);
33+
}
34+
else
35+
{
36+
cell.SetCellValue(value);
37+
}
38+
}
39+
40+
rowIndex++;
41+
}
42+
43+
return workbook;
44+
}
45+
46+
private static List<string> ParseCsvLine(string line)
47+
{
48+
var values = new List<string>();
49+
var currentValue = new StringBuilder();
50+
bool inQuotes = false;
51+
52+
for (int i = 0; i < line.Length; i++)
53+
{
54+
char c = line[i];
55+
56+
if (c == '"')
57+
{
58+
if (inQuotes && i + 1 < line.Length && line[i + 1] == '"')
59+
{
60+
currentValue.Append('"');
61+
i++;
62+
}
63+
else
64+
{
65+
inQuotes = !inQuotes;
66+
}
67+
}
68+
else if (c == ',' && !inQuotes)
69+
{
70+
values.Add(currentValue.ToString());
71+
currentValue.Clear();
72+
}
73+
else
74+
{
75+
currentValue.Append(c);
76+
}
77+
}
78+
79+
values.Add(currentValue.ToString());
80+
return values;
81+
}
82+
}

src/Plugins/BotSharp.Plugin.ExcelHandler/Services/MySqlService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,13 @@ private string ParseSheetData(ISheet singleSheet)
162162
private List<string> ParseSheetColumn(ISheet sheet)
163163
{
164164
if (sheet.PhysicalNumberOfRows < 2)
165+
{
165166
throw new Exception("No data found in the excel file");
167+
}
166168

167169
_excelRowSize = sheet.PhysicalNumberOfRows - 1;
168170
var headerRow = sheet.GetRow(0);
169-
var headerColumn = headerRow.Cells.Select(x => x.StringCellValue.Replace(" ", "_")).ToList();
171+
var headerColumn = headerRow.Cells.Where(x => !string.IsNullOrWhiteSpace(x.StringCellValue)).Select(x => x.StringCellValue.Replace(" ", "_")).ToList();
170172
_excelColumnSize = headerColumn.Count;
171173
return headerColumn;
172174
}

src/Plugins/BotSharp.Plugin.ExcelHandler/Using.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
global using BotSharp.Plugin.ExcelHandler.Models;
2828
global using BotSharp.Plugin.ExcelHandler.Services;
2929
global using BotSharp.Plugin.ExcelHandler.Settings;
30+
global using BotSharp.Plugin.ExcelHandler.Helpers;
3031

3132
global using Microsoft.Extensions.Logging;
3233
global using Microsoft.Extensions.DependencyInjection;

0 commit comments

Comments
 (0)