首页 新闻 会员 周边

EXCLE导入SQL 中文读不出

0
悬赏园豆:20 [已解决问题] 解决于 2010-03-15 16:16

不知道为什么就是上传来的EXCLE文件就里面的中文读取的时候都是空的,数字与英文就没有问题!各位帮忙看下!

第一个:

事件:

代码
#region 显示EXCEL内容
/// <summary>
/// 显示EXCEL内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void SelectExc(object sender, AjaxEventArgs e)
{
DataTable dt
= GetDataTable(hid_filePath.Text, cmb_tableName.SelectedItem.Value);//路径与EXCLE中SHEEL名
StoreDatabing.DataSource = dt;
StoreDatabing.DataBind();

}
#endregion
第二:事件调用的方法

代码
#region 封装数据
private DataTable GetDataTable(string filePath, string tableName)
{
ExcelReader reader
= null;
reader
= new ExcelReader();
reader.Path
= filePath;
reader.TableName
= tableName;
reader.OpenExcel();
DataTable dt
= reader.GetTable();
return dt;
}
#endregion
第三:实现类

代码
using System;
using System.Data;
using System.Data.OleDb;

namespace Bo
{
/// <summary>
/// 读取Excel文件,并转换成DataTable对象。
/// </summary>
public class ExcelReader : IDisposable
{
protected OleDbConnection _conn;
protected OleDbDataAdapter _da;
protected DataTable _dt;
protected string _connString;
protected string _sql;

/// <summary>
/// 构造函数,初始化数据库连接对象;
/// </summary>
public ExcelReader()
{
_conn
= new OleDbConnection();
_connString
= "";
_sql
= "";
}

public ExcelReader( string connString )
{
_conn
= new OleDbConnection( );
_connString
= connString;
_sql
= "";
}

/// <summary>
/// 设置Excel文件的所在路径。
/// </summary>
public string Path
{
set
{
_connString
= "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + value + "; Extended Properties=Excel 8.0;";
}
}

/// <summary>
/// 设置Excel文件中的表的名称,一般为[sheet1$]
/// </summary>
public string TableName
{
set{ _sql = "select * from [" + value + "]"; }
}

/// <summary>
/// 打开Excel文件。
/// </summary>
public void OpenExcel()
{
if( _connString == "" ) throw new Exception("未设置文件路径。");
else if( _sql == "" ) throw new Exception("未设置Excel表格名称。");
else
{
_conn.ConnectionString
= _connString;
_conn.Open();
}
}

/// <summary>
/// 获取Excel表格转换后的DataTable对象。
/// </summary>
public DataTable GetTable()
{
_da
= new OleDbDataAdapter( _sql, _conn );
_dt
= new DataTable( "Excel" );
_da.Fill( _dt );
_conn.Close();
return _dt;
}

/// <summary>
/// 销毁对象。
/// </summary>
public void Dispose()
{
if( _dt != null ) _dt.Dispose();
if( _da != null ) _da.Dispose();
if( _conn != null ) _conn.Dispose();
GC.SuppressFinalize(
this );
}

public static DataTable GetExcelSheet(string strPath)
{

OleDbConnection ExcelConnection
= new OleDbConnection(@"Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + strPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'");
OleDbCommand ExcelCommand
= new OleDbCommand();
ExcelCommand.Connection
= ExcelConnection;
OleDbDataAdapter ExcelAdapter
= new OleDbDataAdapter(ExcelCommand);

ExcelConnection.Open();
DataTable ExcelSheets
= ExcelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });

ExcelConnection.Close();
return ExcelSheets;

}
}
}

 

鑫的主页 | 初学一级 | 园豆:2
提问于:2010-03-11 19:30
< >
分享
最佳答案
0

在导入数据连接字符串中,将IMEX=1加入,“Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Data.xls";Extended Properties="Excel 8.0;HDR=Yes;IMEX=1; ”,这样就可以。

C#读取Excel需要注意的:

IMEX=1:混合模式

HDR=Yes; 是否让第一行作为列头

两者必须一起使用。

收获园豆:10
风影极光 | 小虾三级 |园豆:1573 | 2010-03-11 21:21
这个我里面有啊!现在查出来由于EXCLE版本问题,不知道有什么办法做成兼容的!
| 园豆:2 (初学一级) | 2010-03-11 21:32
其他回答(2)
0

看了你说要做成兼容的,个人想法这样子来处理看 成不啊?

先创建一个Excel的实例,然后取这个实例的Version,然后根据不同的Version来配置相应的Extended Properties值以及连接字符串,再来实现相应的读取代码。

收获园豆:5
西越泽 | 园豆:10775 (专家六级) | 2010-03-11 22:55
0

最好让客户上传上来的excel都经过转换,最好转换成csv,一般客户拷来拷去,文件容易变的混乱,这种转换一般人都会.完全没有必要去处理多种格式的情况

收获园豆:5
bmrxntfj | 园豆:301 (菜鸟二级) | 2010-03-12 16:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册