c#读取Excel 数据
using System;
using System.Data;
using Retail.DataEntity;
using Retail.SqlServerDAL;
using Excel;
using Retail.SqlServerDAL.common;
using System.IO;
namespace Retail.BussinessRules
{
/// <summary>
/// Summary description for excelOutRule.
/// </summary>
public class excelOutRule
{
public excelOutRule()
{
//
// TODO: Add constructor logic here
//
}
public string excelSalesPpsr(string strPath,System.Data.DataTable table)
{
Excel.Application excel = new Excel.Application(); //生成excel对象
Excel.Workbook myBook;
Excel.Worksheet mySheet;
Excel.Range rangeTemp;
object missing = System.Reflection.Missing.Value;//生成错误信息值,信息值为空
string cellcol_pos = "";//excel的range记录
StatisticRules statisticrules = new StatisticRules();
// System.Data.DataTable table = new System.Data.DataTable();
//
// table = statisticrules.SaleSelectTable(statisticentity);
string excelsqltemp = OA_Config.AppPath +@"\excelTemp\" + DateTime.Today.ToString("yyyyMMdd")+
new Random(DateTime.Now.Millisecond).Next(10000).ToString() +".tmp";
FileStream fs=new FileStream(excelsqltemp,FileMode.Create,FileAccess.Write);
fs.Close();
System.IO.File.Copy( strPath, @excelsqltemp, true );
System.IO.File.SetAttributes( @excelsqltemp, System.IO.FileAttributes.Normal );
try
{
excel.Workbooks.Open(excelsqltemp,missing,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing,missing);
myBook = excel.Workbooks[1];
mySheet = (Excel.Worksheet)myBook.Worksheets[1];
for(int i=0;i<table.Rows.Count;i++)
{
cellcol_pos = "B" + System.Convert.ToString(i+4);
rangeTemp = mySheet .get_Range(cellcol_pos,cellcol_pos);
rangeTemp.Value2 = table.Rows[i]["PAMon1"].ToString();
cellcol_pos = "C" + System.Convert.ToString(i+4);
rangeTemp = mySheet .get_Range(cellcol_pos,cellcol_pos);
rangeTemp.Value2 = table.Rows[i]["AAMon1"].ToString();
cellcol_pos = "E" + System.Convert.ToString(i+4);
rangeTemp = mySheet .get_Range(cellcol_pos,cellcol_pos);
rangeTemp.Value2 = table.Rows[i]["PAMon2"].ToString();
cellcol_pos = "F" + System.Convert.ToString(i+4);
rangeTemp = mySheet .get_Range(cellcol_pos,cellcol_pos);
rangeTemp.Value2 = table.Rows[i]["AAMon2"].ToString();
cellcol_pos = "H" + System.Convert.ToString(i+4);
rangeTemp = mySheet .get_Range(cellcol_pos,cellcol_pos);
rangeTemp.Value2 = table.Rows[i]["PAMon3"].ToString();
cellcol_pos = "I" + System.Convert.ToString(i+4);
rangeTemp = mySheet .get_Range(cellcol_pos,cellcol_pos);
rangeTemp.Value2 = table.Rows[i]["AAMon3"].ToString();
}
excel.ActiveWorkbook.Save();
excel.Application.Quit();
excel.Quit();
GC.Collect();
}
catch(Exception ex)
{
MessageLog.WriteLog(ex.ToString());
throw(ex);
}
finally
{
GC.Collect();
}
return excelsqltemp;
}
方法2:
相当简单,Excel就像数据库,每个Sheet就是一个Table. Microsoft.Jet.OLEDB驱动.
之后是DataReader循环,或DataSet处理都非常简单.
注意:数据类型的转换!!
#region set connection
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "+this.txtPath.Text+";Extended Properties=Excel 8.0;";
myDataReader = null;
craboDbConnection = new OleDbConnection(strConn);
OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", myOleDbConnection);
#endregion
try
{
myOleDbConnection.Open();
myDataReader = myOleDbCommand.ExecuteReader();
while (myDataReader.Read())
{
this.txtSeq.Text=Convert.ToString(myDataReader.GetValue(0));//列1
this.txtName.Text=Convert.ToString(myDataReader.GetValue(1));//列2
this.txtPIN.Text=Convert.ToString(myDataReader.GetValue(2));//列3
}
}
#region Catch
catch(System.Threading.ThreadAbortException e)
{
System.Threading.Thread.ResetAbort();
this.lblResult.Text = "线程被中断..."+e.Message;
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
finally
{
// Always call Close when done reading.
if (myDataReader != null)
myDataReader.Close();
// Close the connection when done with it.
if (craboDbConnection!=null && craboDbConnection.State == ConnectionState.Open)
craboDbConnection.Close();
if(webResponse!=null)
webResponse.Close();
}
#endregion

浙公网安备 33010602011771号