读书人

c# Excel导入内存有关问题

发布时间: 2013-04-02 12:35:26 作者: rapoo

c# Excel导入内存问题
有一个5000列的Excel,想导入内存,但是为啥导进来的DataTalbe只有255列
DataTable有这个限制吗,代码如下

public DataSet ImportExcel(string ExcelName)
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelName + ";Extended Properties='Excel 12.0;HDR=No;IMEX=1';";//连接excel文件的字符串

if (ExcelName == null)
{
return null;
}
OleDbConnection odcon = new OleDbConnection(strConn);//建立连接
try
{
odcon.Open();//打开连接
}
catch(Exception ex)
{

}

System.Data.DataTable sTable = odcon.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//return sTable;
//Sheets Name
string tableName = sTable.Rows[1][2].ToString().Trim();
if (tableName == "")
{
return null;
}
else
{
tableName = "[" + tableName + "]";
}
OleDbDataAdapter odda = new OleDbDataAdapter("select * from " + tableName, odcon);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
try
{
odda.Fill(ds);
}
catch (Exception ex)


{
//MessageBox.Show(ex.Message);
}
return ds;
}
[解决办法]
楼主可以试一下开源Excel组件,Aspose,NPOI
1.MyXls(http://sourceforge.net/projects/myxls/)
2.Koogra(http://sourceforge.net/projects/koogra/)
3.ExcelLibrary(http://code.google.com/p/excellibrary/)
4.ExcelPackage(http://excelpackage.codeplex.com/)
5.EPPlus(http://epplus.codeplex.com/)
6.LinqToExcel(http://code.google.com/p/linqtoexcel/)
7.NetOffice(http://netoffice.codeplex.com/) 需安装Office Excel

读书人网 >C#

热点推荐