如何把DataGridView中的数据导入Excel
2010-05-12 15:32
411 查看
现在很多客户都要求软件实现把DataGridView中的数据导入到Excel这项功能,现在网上很多方法都需要添加一些组件,但是本人感觉没有必要,而且编写的代码比较复杂,现在把一种简单的方法说出来,以飨读者!源代码如下:
//buttonToExcel是button的ID
private void buttonToExcel_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
//Excel文件保存的路径初始化为D盘,根据需要可以做适当的修改
saveFileDialog.InitialDirectory = "D://";
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Excel File To";
saveFileDialog.ShowDialog();
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < this.dataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "/t";
}
str += this.dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < this.dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "/t";
}
tempStr += this.dataGridView1.Rows[j].Cells[k].Value + "";
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
MessageBox.Show("导入Excel成功!");
}
//buttonToExcel是button的ID
private void buttonToExcel_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
//Excel文件保存的路径初始化为D盘,根据需要可以做适当的修改
saveFileDialog.InitialDirectory = "D://";
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "Export Excel File To";
saveFileDialog.ShowDialog();
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < this.dataGridView1.ColumnCount; i++)
{
if (i > 0)
{
str += "/t";
}
str += this.dataGridView1.Columns[i].HeaderText;
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
{
string tempStr = "";
for (int k = 0; k < this.dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "/t";
}
tempStr += this.dataGridView1.Rows[j].Cells[k].Value + "";
}
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
MessageBox.Show("导入Excel成功!");
}
相关文章推荐
- 如何从Excel中把数据导入到SharePoint List(Import Excel data to SharePoint List)
- 如何把excel中的数据导入到SQL 2000数据库中
- 如何把excel中的数据导入到oracle数据已有的表中
- 导入excel 文件解析,一行数据,有一列分两行显示,解析时如何 合为一行?
- 如何用Pl*SQL将Excel中数据导入到oracle中
- 如何提高C#中将数据导入到Excel的性能
- swing中如何将jtable中的数据导入到excel中?
- 超级简单:如何更快的将数据导入Excel
- C#Winform 将DataGridView中的数据导入到Excel中
- excel表格中的数据如何放到数据库中(非导入)
- 如何将excel数据导入mongoDB数据库
- DataGridView数据导入到Excel 中
- C#.NET常见问题(FAQ)-如何使用DataGridView跟Excel数据交互
- 如何将 Excel 数据导入 SQL Server
- 在.net中如何把数据导入到Excel
- 如何把MYSQL中的数据导入EXCEL
- MATLAB/Excel-如何将Excel数据导入MATLAB中
- 如何将excel数据导入mongoDB数据库?
- ORACLE学习之路--如何将EXCEL中的数据导入ORACLE
- 如何将Excel表中的数据导入到数据库