今天的天气真不错,比昨天好多了,风也不大。
今天主要完成昨天那些未完成的工作。下面是今天查到的一些比较有用的资料:
1. 读取Excel文件:
DataSet dsTaskList = new DataSet();
string sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Data.xls;Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
OleDbConnection oleConn = new OleDbConnection(sConn);
oleConn.Open();
string sCommandText = "select * from [" + sheet + "]";
OleDbDataAdapter oleCommand = new OleDbDataAdapter(sCommandText, sConn);
oleCommand.Fill(dsTaskList, "dtTaskList");
oleConn.Close();
oleConn.Dispose();
string sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Data.xls;Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
OleDbConnection oleConn = new OleDbConnection(sConn);
oleConn.Open();
DataTable dtSheet = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string sheet = dtSheet.Rows[0]["Table_Name"].ToString();
string sCommandText = "select * from [" + sheet + "]";
OleDbDataAdapter oleCommand = new OleDbDataAdapter(sCommandText, sConn);
oleCommand.Fill(dsTaskList, "dtTaskList");
oleConn.Close();
oleConn.Dispose();
2. 写入Excel文件:
//StreamWrite:xlsStream把DataTable:dtData的数据写入Excel文件
string sTable = "";
//标题
for (int i = 0; i < dtData.Columns.Count; i++)
{
if (i > 0)
{
sTable += "\t";
}
sTable += dtData.Columns[i].Caption;
}
xlsStream.WriteLine(sTable );
//内容
for (int j = 0; j < dtData.Rows.Count; j++)
{
sTable = "";
for (int k = 0; k < dtData.Columns.Count; k++)
{
if (k > 0) tempStr += "\t";
sTable += dtData.Rows[j][k].ToString();
}
xlsStream.WriteLine(sTable );
}
string sTable = "";
//标题
for (int i = 0; i < dtData.Columns.Count; i++)
{
if (i > 0)
{
sTable += "\t";
}
sTable += dtData.Columns[i].Caption;
}
xlsStream.WriteLine(sTable );
//内容
for (int j = 0; j < dtData.Rows.Count; j++)
{
sTable = "";
for (int k = 0; k < dtData.Columns.Count; k++)
{
if (k > 0) tempStr += "\t";
sTable += dtData.Rows[j][k].ToString();
}
xlsStream.WriteLine(sTable );
}
在模仿中成长,在创新中成功