Skip to content

Commit

Permalink
修改 csv 导出后 换行问题引发的bug 及 数字会自动变科学计数法问题引发的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
toolgood committed Oct 28, 2021
1 parent da4f4c1 commit afa3377
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
36 changes: 33 additions & 3 deletions Source/ToolGood.SqlOnline/Pages/Sqls/Editors/AjaxController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*/
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using ToolGood.Common.Extensions;
Expand Down Expand Up @@ -208,7 +209,7 @@ private string BuildCsv(ExecuteResult result)
foreach (var resultItem in result.Result) {
for (int i = 0; i < resultItem.Columns.Count; i++) {
var col = resultItem.Columns[i];
stringBuilder.Append(col);
stringBuilder.Append(MakeValueCsvFriendly(col));
if (i < resultItem.Columns.Count - 1) {
stringBuilder.Append(',');
}
Expand All @@ -218,7 +219,7 @@ private string BuildCsv(ExecuteResult result)
foreach (var values in resultItem.Values) {
for (int i = 0; i < values.Length; i++) {
var col = values[i];
stringBuilder.Append(col);
stringBuilder.Append(MakeValueCsvFriendly(col));
if (i < resultItem.Columns.Count - 1) {
stringBuilder.Append(',');
}
Expand All @@ -232,6 +233,35 @@ private string BuildCsv(ExecuteResult result)
return stringBuilder.ToString();
}

private string MakeValueCsvFriendly(object value, string columnSeparator = ",")
{
if (value == null) return "";
if (value is INullable && ((INullable)value).IsNull) return "";

string output;
if (value is DateTime) {
if (((DateTime)value).TimeOfDay.TotalSeconds == 0) {
output = ((DateTime)value).ToString("yyyy-MM-dd");
} else {
output = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
}
} else {
var val= value.ToString().Trim();
if (Regex.IsMatch(val,@"^\d{12,}(\.\d+)?$",RegexOptions.Compiled)) {
return "=\"" + val + "\"";
}
output = val;
}

if (output.Length > 30000) //cropping value for stupid Excel
output = output.Substring(0, 30000);

if (output.Contains(columnSeparator) || output.Contains("\"") || output.Contains("\n") || output.Contains("\r"))
output = '"' + output.Replace("\"", "\"\"") + '"';

return output;
}

private string BuildJson(ExecuteResult result)
{
if (result.Result == null) { return "[]"; }
Expand Down
10 changes: 4 additions & 6 deletions Source/ToolGood.SqlOnline/Pages/Sqls/Editors/Export.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
/*!
* °æȨËùÓÐ(C) 2021 ToolGood(ÁÖÖª¾ý)
* GPLv3 License - http://www.gnu.org/licenses/gpl-3.0.html
*/

namespace ToolGood.SqlOnline.Pages.Sqls.Editors
{
Expand Down

0 comments on commit afa3377

Please sign in to comment.