Skip to content

Commit

Permalink
Fix asp.net webform gridview datasource can't use miniexcel queryasda…
Browse files Browse the repository at this point in the history
…tatable #223
  • Loading branch information
shps951023 committed May 7, 2021
1 parent 8f3d521 commit 64071b1
Show file tree
Hide file tree
Showing 19 changed files with 451 additions and 12 deletions.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

---

### 0.14.2
- [Bug] Fix asp.net webform gridview datasource can't use QueryAsDataTable #223

### 0.14.1
- [Bug] Fix custom m/d format not convert datetime #222

Expand Down
3 changes: 3 additions & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

---

### 0.14.2
- [Bug] 修正 asp.net webform gridview datasource 不能使用 QueryAsDataTable #223

### 0.14.1
- [Bug] 修正自定义 m/d 格式没转成 datetime #222

Expand Down
3 changes: 3 additions & 0 deletions docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

---

### 0.14.2
- [Bug] 修正 asp.net webform gridview datasource 不能使用 QueryAsDataTable #223

### 0.14.1
- [Bug] 修正自定義 m/d 格式沒轉成 datetime #222

Expand Down
23 changes: 16 additions & 7 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static ICollection<string> GetColumns(string path, bool useHeaderRow = fa

public static ICollection<string> GetColumns(this Stream stream, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
{
return (Query(stream, useHeaderRow,sheetName,excelType,startCell,configuration).FirstOrDefault() as IDictionary<string, object>)?.Keys;
return (Query(stream, useHeaderRow, sheetName, excelType, startCell, configuration).FirstOrDefault() as IDictionary<string, object>)?.Keys;
}

public static void SaveAsByTemplate(string path, string templatePath, object value)
Expand Down Expand Up @@ -114,28 +114,37 @@ public static DataTable QueryAsDataTable(string path, bool useHeaderRow = true,

/// <summary>
/// This method is not recommended, because it'll load all data into memory.
/// Note: if first row cell is null value, column type will be string
/// </summary>
public static DataTable QueryAsDataTable(this Stream stream, bool useHeaderRow = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
{
var dt = new DataTable();
dt.TableName = sheetName;
if (sheetName == null)
sheetName = stream.GetSheetNames().First();

var dt = new DataTable(sheetName);
var first = true;
var rows = ExcelReaderFactory.GetProvider(stream, GetExcelType(stream, excelType)).Query(useHeaderRow, sheetName,startCell, configuration);
var rows = ExcelReaderFactory.GetProvider(stream, GetExcelType(stream, excelType)).Query(useHeaderRow, sheetName, startCell, configuration);
foreach (IDictionary<string, object> row in rows)
{
if (first)
{
foreach (var key in row.Keys)
{
//TODO:base on cell t xml
//var type = row[key]?.GetType() ?? typeof(object);
var type = typeof(object);
// issue 223 : can't assign typeof object
var type = row[key]?.GetType() ?? typeof(string);
if (type == null || type == typeof(object))
type = typeof(string);
dt.Columns.Add(key, type);
}

first = false;
}
dt.Rows.Add(row.Values.ToArray());

var newRow = dt.NewRow();
foreach (var key in row.Keys)
newRow[key] = row[key];
dt.Rows.Add(newRow);
}
return dt;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31205.134
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebFormTest", "WebFormTest\WebFormTest.csproj", "{47EE42A4-E8DA-4355-A910-147E725C618B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniExcelLibs", "..\..\src\MiniExcel\MiniExcelLibs.csproj", "{AC525AB6-2A1A-4935-934B-322C92FC4A94}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{47EE42A4-E8DA-4355-A910-147E725C618B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47EE42A4-E8DA-4355-A910-147E725C618B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47EE42A4-E8DA-4355-A910-147E725C618B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47EE42A4-E8DA-4355-A910-147E725C618B}.Release|Any CPU.Build.0 = Release|Any CPU
{AC525AB6-2A1A-4935-934B-322C92FC4A94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AC525AB6-2A1A-4935-934B-322C92FC4A94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC525AB6-2A1A-4935-934B-322C92FC4A94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC525AB6-2A1A-4935-934B-322C92FC4A94}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {27C1DF70-1D3C-4D03-BC5B-5A8E66C85879}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/Global.asax
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="WebFormTest.Global" Language="C#" %>
16 changes: 16 additions & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace WebFormTest
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WebFormTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WebFormTest")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("47ee42a4-e8da-4355-a910-147e725c618b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
17 changes: 17 additions & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestIssue223.aspx.cs" Inherits="WebFormTest.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
</asp:GridView>
</div>
</form>
</body>
</html>
26 changes: 26 additions & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/TestIssue223.aspx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFormTest
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var path = HttpContext.Current.ApplicationInstance.Server.MapPath("~/TestIssue223.xlsx");
var dt = MiniExcelLibs.MiniExcel.QueryAsDataTable(path);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
30 changes: 30 additions & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/Web.Debug.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
31 changes: 31 additions & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/Web.Release.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
17 changes: 17 additions & 0 deletions tests/MiniExcel.Tests.WebForm/WebFormTest/Web.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
Loading

0 comments on commit 64071b1

Please sign in to comment.