您的位置:首页 > 编程语言 > C#

c#将datagridview信息导入excel

2016-03-31 11:42 381 查看
我们需要添加引用using Excel = Microsoft.Office.Interop.Excel;

1.可以设置导出的excel文件的名字,包含导出位置窗体

private void ExportExcel(string fileName, DataGridView myDGV, ToolStripProgressBar tempProgressBar, ToolStripStatusLabel toolstrip)
{
string saveFileName = "";
bool fileSaved = false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //被点了取消
tempProgressBar.Visible = true;
tempProgressBar.Minimum = 1;
tempProgressBar.Maximum = myDGV.RowCount;
tempProgressBar.Step = 1;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
if (excel == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = excel.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 < myDGV.ColumnCount; i++)
{
worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
}
//写入数值
for (int r = 0; r < myDGV.Rows.Count; r++)
{
for (int i = 0; i < myDGV.ColumnCount; i++)
{
worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
}
System.Windows.Forms.Application.DoEvents();
toolstrip.Text = "|| 状态:正在生成第 " + r + "/" + myDGV.RowCount + " 个";
tempProgressBar.Value = r + 1;
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
else
{
fileSaved = false;
}
excel.Quit();
GC.Collect();//强行销毁
toolstrip.Text = "|| 状态:生成成功!";
if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
MessageBox.Show(fileName + "的简明资料保存成功", "提示", MessageBoxButtons.OK);
}


2.直接的简单保存,并打开文件

public void DataToExcel(DataGridView dgv, ToolStripProgressBar tempProgressBar, ToolStripStatusLabel toolstrip)
{
if (dgv.Rows.Count == 0)
{
MessageBox.Show("无数据"); return;
}
MessageBox.Show("开始生成要导出的数据", "导出提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = false;
for (int i = 0; i < dgv.ColumnCount; i++)
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
tempProgressBar.Visible = true;
tempProgressBar.Minimum = 1;
tempProgressBar.Maximum = dgv.RowCount;
tempProgressBar.Step = 1;
toolstrip.Visible = true;
for (int i = 0; i < dgv.RowCount; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
}
}
toolstrip.Text = "|| 状态:正在生成第 " + i + "/" + dgv.RowCount + " 个";
tempProgressBar.Value = i + 1;
}
toolstrip.Text = "|| 状态:生成成功!";
MessageBox.Show("生成成功,请保存。", "生成提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
excel.Visible = true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: