将DataTable里的内容导出到Excel

private void SaveToExcel()
{
SaveFileDialog SFDia = new SaveFileDialog();
SFDia.Filter = "Exel files (*.xls)|*.xls";
SFDia.FilterIndex = 2;
SFDia.RestoreDirectory = true;
SFDia.OverwritePrompt = true;
SFDia.FileName = "";
SFDia.ShowDialog();
string FileName = SFDia.FileName;
if (FileName == "")
return;
FileStream objFileStream;
StreamWriter objStreamWriter;
string strLine = "";
objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
for (int i = 0; i < FTableResult.Columns.Count; i++)
{
strLine = strLine + FTableResult.Columns[i].ColumnName.ToString() + Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine = "";
for (int i = 0; i < FTableResult.Rows.Count; i++)
{
for (int j = 0; j < FTableResult.Columns.Count; j++)
{
strLine = strLine + FTableResult.Rows[i][j].ToString() + Convert.ToChar(9);
}
objStreamWriter.WriteLine(strLine);
strLine = "";
}
objStreamWriter.Close();
objFileStream.Close();

}

你可能感兴趣的:(Datatable)