//定义变量
string winDir;
//文件路径
string filename;
//结果文件名
string [] filename2=new string[300];
//保存生成的文件名
string temp1,temp2,teMP3,temp4;
string strCon;
//连接串
int ii;
//统计生成的结果文件数
ii=1;
winDir=textBox2.Text.Trim()+@"\xcsj.xls;";
//创建一个数据链接
strCon= " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source ="+winDir+"Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
//创建一个
DataSet对象 DataSet myDataSet = new DataSet ( ) ;
//得到自己的DataSet对象
myDataSet
try
{
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
} catch( Exception err)
{
MessageBox.Show(err.Message);
}
//根据电子表格中第一列的井号读出井名到数组中去
temp1=myDataSet.Tables[0].Rows[0][0].ToString().Trim();
filename2[ii]=temp1;
foreach ( DataRow row in myDataSet.Tables[0].Rows )
{
if (temp1!=row[0].ToString().Trim() )
{
ii=ii+1;
filename2[ii]=row[0].ToString();
temp1=row[0].ToString().Trim();
}
}
//进度条初始化
progressBar1.Step=1;
progressBar1.Minimum=1;
progressBar1.Maximum=ii;
winDir=textBox2.Text.Trim();
for(int k=1;k<=ii;k++)
{
filename=@winDir+"\\"+filename2[k].Trim()+".xc";
StreamWriter writer = new StreamWriter(filename);
writer.WriteLine("层位 细分小层号 层顶深度 层厚");
foreach( DataRow row in myDataSet.Tables[0].Rows )
{
temp1=row[1].ToString().Trim();
temp2=row[2].ToString().Trim();
temp3=row[3].ToString().Trim();
temp4=row[4].ToString().Trim();
writer.WriteLine(temp1+" "+temp2+" "+temp3+" "+temp4);
}
writer.Close();
progressBar1.PerformStep();
}
myConn.Close();
addListItem("操作结果");
addListItem("共生成"+ii.ToString()+"文件");
addListItem(@"文件位置:F:\wangwei\work\h12\xc路径下");
listBox1.Refresh();