我现在想把数据从excel中全取出来然后放到一个集合中,以后将取出来的数据拿来用时就不用再次连接数据库了,现在没有想到一个好的方法还请各位提示一下。
解决方案 »
- 数据库与实体真的是一一对应吗?为啥学生-平均成绩这个例子与结论相反?
- datagridview 边查询边显示
- blog 中 Calendar控件问题
- 咋连接sql server ?
- 如何向Access的“OLE对象”字段中存取图片?急!
- 求助C#.net+ACCESS论坛系统源程序
- .net应用程序有32位和64位之分?
- System.Windows.Controls.DataGrid 在.net 3.5 下不可用?
- 请问怎样让一个form有垂直滚动条和水平滚动条?
- 在一个Console程序中,如何得到程序自身的名称,程序的所在目录?
- C#关于GIF动态图片与二进制转换问题
- 服务器上的XML文件如何保存到本地
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open(); DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
DataSet ds = new DataSet();
//一个EXCEL文件可能有多个工作表,遍历之
foreach (DataRow dr in dtSchema.Rows)
{
string table = dr["TABLE_NAME"].ToString();
string strExcel = "SELECT * FROM [" + table + "]";
ds.Tables.Add(table);
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, conn);
myCommand.Fill(ds, table); //()
}
/// 导入EXCEL到DataSet
/// </summary>
/// <param name="fileName">Excel全路径文件名</param>
/// <returns>导入成功的DataSet</returns>
public DataSet doImport(string fileName)
{
//判断是否安装EXCEL
Microsoft.Office.Interop.Excel.Application xlApp=new Microsoft.Office.Interop.Excel.Application();
if(xlApp==null)
{
MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel","消息提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Error);
return null;
}
//判断文件是否被其他进程使用
Microsoft.Office.Interop.Excel.Workbook workbook;
try
{
//workbook = xlApp.Workbooks.Open(fileName,0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
workbook = xlApp.Workbooks.Open(fileName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, missing, missing, missing, missing, missing, missing, missing);
}
catch
{
MessageBox.Show("Excel文件处于打开状态,请保存关闭","消息提示",MessageBoxButtons.RetryCancel,MessageBoxIcon.Error);
return null;
} //获得所有Sheet名称
int n = workbook.Worksheets.Count;
string[] SheetSet = new string[n];
System.Collections.ArrayList al = new System.Collections.ArrayList();
for(int i=1; i<=n; i++)
{
SheetSet[i-1] = ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[i]).Name;
} //释放Excel相关对象
workbook.Close(null,null,null);
xlApp.Quit();
if(workbook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
}
if(xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect(); //把EXCEL导入到DataSet
DataSet ds = new DataSet();
string ext = Path.GetExtension(fileName).ToLower();
string strConn="";
if (ext == ".xls")
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
}
else if (ext == ".xlsx")
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES' ";
}
using (OleDbConnection conn = new OleDbConnection(strConn))
{
try
{
conn.Open();
OleDbDataAdapter da;
for (int i = 1; i <= n; i++)
{
string sql = "select * from [" + SheetSet[i - 1] + "$] ";
da = new OleDbDataAdapter(sql, conn);
da.Fill(ds, SheetSet[i - 1]);
da.Dispose();
}
}
catch (Exception e)
{
MessageBox.Show("Excel文件不是预期的版本", "消息提示", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
}
finally
{
conn.Close();
conn.Dispose();
}
}
return ds;
}
貌似有很多容器可以解决类似问题.List Dictionary 等等