VS2005中 GridView导入Excel的导入需要注意的几点
最近带的项目遇到GridView导入Excel问题,总结出几点:
1、如果出现下面的错误提示可用重载VerifyRenderingInServerForm方法解决。
错误提示:
类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
在后台文件中重载VerifyRenderingInServerForm方法,如:
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
{
//base.VerifyRenderingInServerForm(control);
}
2、如果设置为 GetEncoding("GB2312"),导出的文件将会出现乱码。
可用Response.ContentEncoding = System.Text.Encoding.UTF7;
或者Encoding.UTF8等来解决,不过导入格式和字体上个人感觉UTF7比UTF8效果好些;
因人而异了:)
相关代码如下:
Web.config配置:
或者Encoding.UTF8等来解决,不过导入格式和字体上个人感觉UTF7比UTF8效果好些;
因人而异了:)
相关代码如下:
Web.config配置:
<?xml version="1.0"?>
<configuration>
<appSettings>
<!--数据库连接串-->
<add key="ConnectionString" value="data source=.;initial catalog=Northwind;user id=sa;password=sa;persist security info=true;packet size=4096"/>
</appSettings>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
ASPX页面代码:<configuration>
<appSettings>
<!--数据库连接串-->
<add key="ConnectionString" value="data source=.;initial catalog=Northwind;user id=sa;password=sa;persist security info=true;packet size=4096"/>
</appSettings>
<connectionStrings/>
<system.web>
<compilation debug="true"/>
<authentication mode="Windows"/>
</system.web>
</configuration>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id=
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id=
浙公网安备 33010602011771号