在网站上看到这样的一个文章。就借鉴过来:
System.Windows.Forms.SaveFileDialog sd=new System.Windows.Forms.SaveFileDialog();
sd.Filter="Excel 2003 文档|*.xls";
if(sd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
FileStream fs=new FileStream(sd.FileName,FileMode.Create,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs,Encoding.Default);
string line="";
//写入列标题
for (int i = 0; i < datamain.Columns.Count; i++)
{
line += datamain.Columns[i].Header.ToString() + "\t";
}
sw.WriteLine(line);
for (int j = 0; j < datamain.Items.Count; j++)
{
Statresult dt = datamain.Items[j] as Statresult;
line = dt.Client + "\t" + dt.Total + "\t" + dt.Receive + "\t" + dt.Rates + "\t" + dt.Inspection + "\t" + dt.Bugnum + "\t" + dt.Qualified + "\t";
sw.WriteLine(line);
}
//关闭流
sw.Close();
fs.Close();
MessageBox.Show("数据已经成功导出!","注意",MessageBoxButton.OK,MessageBoxImage.Information);
}
读取excel:
strConn = "Provider=Microsoft.Jet. OLEDB.4.0;Data Source=C:\\Erp1912.xls;Extended Properties='Excel8.0;HDR=Yes;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strCon);
//将工作表作为数据表
string sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter adp = new OleDbDataAdapter(sql,conn);
DataSet myDataSet = new DataSet();
adp.Fill(myDataSet,"[Sheet1$]");
本文来自何苦上青天的博客,原文地址:http://www.cnblogs.com/861580030/archive/2011/01/14/1935246.html