/*文件保存对话框,文件保存采用了一个file自定义类*/
SaveFileDialog saveFileDialog=new SaveFileDialog();
saveFileDialog.Filter= "Excel files(*.xls)|*.xls|All files(*.*)|*.*" ;
if(origin=="CallRecord")
{
saveFileDialog.Title="月末电话数据导入Excel";
}
else saveFileDialog.Title="月末信息发布数据导入Excel";
saveFileDialog.FilterIndex=1;
saveFileDialog.RestoreDirectory=true;
if(saveFileDialog.ShowDialog()==DialogResult.OK)
{
string fName=saveFileDialog.FileName;
File fSaveAs=new File(fName);
fSaveAs.WriteFile(fName);
}//if()
excel.Workbooks.Close();
excel.Quit();//关闭excel 程序
}//try
catch(System.Exception e)
{
System.Console.WriteLine("something wrong happened about excel excution or dababase operation ",e);
}
}//connect
/*清空当前表内容*/
private void DeleteRecord(string record)
{
SqlConnection connection=creCon();
connection.Open();
string DeleteString= "delete from "+record;
SqlCommand deleteCommand=new SqlCommand(DeleteString,connection);
deleteCommand.ExecuteNonQuery();
connection.Close();
}
/*数据库连接函数*/
public static SqlConnection creCon()
{
string sql= "server=127.0.0.1;uid=sa;pwd=;database=zhaoxia";
SqlConnection con=new SqlConnection (sql);
return con;
}
/*主函数*/
static void Main ( )
{
new IntroduceExcel ( ) ;
}
/*文件操作类定义*/
public class File
{
string fileName;
public File(string fileName)
{
this.fileName=fileName;
}
public string ReadFile()
{
try
{
StreamReader sr=new StreamReader(fileName,Encoding.Default);
string result=sr.ReadToEnd();
sr.Close();
return result;
}
catch(Exception e){MessageBox.Show(e.Message);}
return null;
}
public void WriteFile(string str)
{
try
{
StreamWriter sw=new StreamWriter(fileName,false,Encoding.Default);
sw.Write(str);
sw.Close();
}
catch(Exception e){MessageBox.Show(e.Message, "保存文件出错!");}
}
}//file类
}