C# 导出DataGridView数据到Excel表
2017-10-12 09:49
465 查看
方法1:
private void button7_Click(object sender, EventArgs e)
{
string fileName = "";
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xlsx";
saveDialog.Filter = "Excel文件|*.xlsx";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
progreesBar.Visible = true;
if (saveFileName.IndexOf(":") < 0) return; //取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
//写入标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{ worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }
//写入数值
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
}
progreesBar.Value += 100 / dataGridView1.Rows.Count;
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
progreesBar.Value = 100;
MessageBox.Show(fileName + "保存成功", "提示", MessageBoxButtons.OK);
progreesBar.Value = 0;
progreesBar.Visible = false;
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName); //fileSaved = true;
}
catch (Exception ex)
{//fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
xlApp.Quit();
GC.Collect();
}
方法二:
流输入
if (dataGridView1.RowCount== 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
progreesBar.Visible = true;
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < dataGridView1.ColumnCount ; i++)
{
if (i > 0)
{
str += "\t";
}
str += dataGridView1.Columns[i].HeaderText.ToString();
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < dataGridView1.RowCount; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridView1.Rows[j].Cells[k].Value;
}
sw.WriteLine(tempStr);
progreesBar.Value += 100 / dataGridView1.Rows.Count;
}
sw.Close();
myStream.Close();
progreesBar.Value = 100;
MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
progreesBar.Value = 0;
progreesBar.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "友情提示", MessageBoxButtons.OK);
}
finally
{
sw.Close();
myStream.Close();
}
}
添加引用 如图
private void button7_Click(object sender, EventArgs e)
{
string fileName = "";
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xlsx";
saveDialog.Filter = "Excel文件|*.xlsx";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
progreesBar.Visible = true;
if (saveFileName.IndexOf(":") < 0) return; //取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
//写入标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{ worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }
//写入数值
for (int r = 0; r < dataGridView1.Rows.Count; r++)
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
}
progreesBar.Value += 100 / dataGridView1.Rows.Count;
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
progreesBar.Value = 100;
MessageBox.Show(fileName + "保存成功", "提示", MessageBoxButtons.OK);
progreesBar.Value = 0;
progreesBar.Visible = false;
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName); //fileSaved = true;
}
catch (Exception ex)
{//fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
xlApp.Quit();
GC.Collect();
}
方法二:
流输入
if (dataGridView1.RowCount== 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
progreesBar.Visible = true;
Stream myStream;
myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string str = "";
try
{
//写标题
for (int i = 0; i < dataGridView1.ColumnCount ; i++)
{
if (i > 0)
{
str += "\t";
}
str += dataGridView1.Columns[i].HeaderText.ToString();
}
sw.WriteLine(str);
//写内容
for (int j = 0; j < dataGridView1.RowCount; j++)
{
string tempStr = "";
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
if (k > 0)
{
tempStr += "\t";
}
tempStr += dataGridView1.Rows[j].Cells[k].Value;
}
sw.WriteLine(tempStr);
progreesBar.Value += 100 / dataGridView1.Rows.Count;
}
sw.Close();
myStream.Close();
progreesBar.Value = 100;
MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
progreesBar.Value = 0;
progreesBar.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "友情提示", MessageBoxButtons.OK);
}
finally
{
sw.Close();
myStream.Close();
}
}
相关文章推荐
- C#使用POI把DataGridView中数据导出到excel文档中
- C#中把当前显示的datagridview中的数据以excel的形式导出
- C#将DataGridView中的数据导出为EXCEL
- C#操作Excel,将DataGridView数据导出到Excel中
- C#之四十三 从DataGridView导出数据到Excel
- C#中DataGridView数据导出到Excel
- C# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据;从Excel导入到DataGridView[转http://yuunagi.blogbus.com/logs/38361242.html]
- C#之四十三 从DataGridView导出数据到Excel
- C#将DataGridView中的数据导出为EXCEL
- C#将dataGridView中显示的数据导出到Excel(大数据量超有用版)
- C#中怎么从如何将DataGridView中的数据导出到Excel中
- C#之四十三 从DataGridView导出数据到Excel
- C#将DataGridView中的数据导出为EXCEL
- 【转】c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
- [转]将c#中datagridview中的数据导出到excel中
- C# WinForm程序中将DataGridView中的数据导出为EXCEL
- WindowsForm程序-导出DataGridView数据到Excel表
- c#中datagridview数据导出到excel源码
- C#操作Excel,将DataGridView数据导出到Excel中
- C# datagridview导出数据至Excel