WinFrom:DataGridView中多维表头如何导出到Excel DataGridView中多维表头如何导出到Excel 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没搞过,DevExpress的表格控件有这个功能 DataGridView没试过,我以前Infragistics第三方控件可以public static void ExPort(string gridtextname, Infragistics.Win.UltraWinGrid.UltraGrid grid, Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter exprot) { try { string exfilename = gridtextname; DialogResult rs = DialogResult.No; SaveFileDialog saved = new SaveFileDialog(); saved.InitialDirectory = "d:\\"; saved.DefaultExt = "*.xls"; //saved.FileName = exfilename.Substring(0, exfilename.Length - 4); saved.FileName = exfilename; saved.Filter = "XLS Files|*.xls"; saved.FilterIndex = 2; saved.RestoreDirectory = true; if (saved.ShowDialog() == DialogResult.OK) { exfilename = saved.FileName; DateTime start = DateTime.Now; exprot.Export(grid, exfilename); TimeSpan timeSpan = DateTime.Now - start; rs = MessageBox.Show("数据导出操作成功,打开文件吗? ", "提示信息 ", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (rs == DialogResult.Yes) { System.Diagnostics.ProcessStartInfo st = new System.Diagnostics.ProcessStartInfo(exfilename); st.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized; System.Diagnostics.Process.Start(st); } } } catch (Exception ex) { Console.Write(ex.Message); MessageBox.Show(ex.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } 谢谢了,可现在就得用DataGridView 多维表头 不太了解 还过倒是刚写了DataGridView导出到Excel的例子 需要么 楼主 给你个例子 但是不是多维的 希望给可以对你有帮助#region DataGridView数据显示到Excel /// <summary> /// 打开Excel并将DataGridView控件中数据导出到Excel /// </summary> /// <param name="dgv">DataGridView对象 </param> /// <param name="isShowExcle">是否显示Excel界面 </param> /// <res> /// Excel数据导入方法 /// 作者:zxx ///时间:2011-5-30 /// add com "Microsoft Excel 11.0 Object Library" /// using Excel=Microsoft.Office.Interop.Excel; /// </res> /// <returns> </returns> public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle) { if (dgv.Rows.Count == 0) return false; //建立Excel对象 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Application.Workbooks.Add(true); excel.Visible = isShowExcle; //生成字段名称 for (int i = 0; i < dgv.ColumnCount; i++) { excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText; } //填充数据 for (int i = 0; i < dgv.RowCount - 1; 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(); } } } return true; } #endregion #region DataGridView导出到Excel,有一定的判断性 /// <summary> ///方法,导出DataGridView中的数据到Excel文件 /// </summary> /// <res> /// /// Excel数据导入方法 /// 作者:zxx /// 时间:2011-5-30 /// 添加 com "Microsoft Excel 11.0 Object Library" /// 命名空间:using Excel=Microsoft.Office.Interop.Excel; /// 命名空间:using System.Reflection; /// </res> /// <param name= "dgv"> DataGridView </param> public static void DataGridViewToExcel(DataGridView dgv) { #region 验证可操作性 //申明保存对话框 SaveFileDialog dlg = new SaveFileDialog(); //默然文件后缀 dlg.DefaultExt = "xls "; //文件后缀列表 dlg.Filter = "EXCEL文件(*.XLS)|*.xls "; //默然路径是系统当前路径 dlg.InitialDirectory = Directory.GetCurrentDirectory(); //打开保存对话框 if (dlg.ShowDialog() == DialogResult.Cancel) return; //返回文件路径 string fileNameString = dlg.FileName; //验证strFileName是否为空或值无效 if (fileNameString.Trim() == " ") { return; } //定义表格内数据的行数和列数 int rowscount = dgv.Rows.Count; int colscount = dgv.Columns.Count; //行数必须大于0 if (rowscount <= 0) { MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //列数必须大于0 if (colscount <= 0) { MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //行数不可以大于65536 if (rowscount > 65536) { MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //列数不可以大于255 if (colscount > 255) { MessageBox.Show("数据记录行数太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //验证以fileNameString命名的文件是否存在,如果存在删除它 FileInfo file = new FileInfo(fileNameString); if (file.Exists) { try { file.Delete(); } catch (Exception error) { MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } #endregion Microsoft.Office.Interop.Excel.Application objExcel = null; Microsoft.Office.Interop.Excel.Workbook objWorkbook = null; Microsoft.Office.Interop.Excel.Worksheet objsheet = null; try { //申明对象 objExcel = new Microsoft.Office.Interop.Excel.Application(); objWorkbook = objExcel.Workbooks.Add(Missing.Value); objsheet = (Microsoft.Office.Interop.Excel.Worksheet)objWorkbook.ActiveSheet; //设置EXCEL不可见 objExcel.Visible = false; //向Excel中写入表格的表头 int displayColumnsCount = 1; for (int i = 0; i <= dgv.ColumnCount - 1; i++) { if (dgv.Columns[i].Visible == true) { objExcel.Cells[1, displayColumnsCount] = dgv.Columns[i].HeaderText.Trim(); displayColumnsCount++; } } //设置进度条 //tempProgressBar.Refresh(); //tempProgressBar.Visible = true; //tempProgressBar.Minimum = 1; //tempProgressBar.Maximum = dgv.RowCount; //tempProgressBar.Step = 1; //向Excel中逐行逐列写入表格中的数据 for (int row = 0; row <= dgv.RowCount - 1; row++) { //tempProgressBar.PerformStep(); displayColumnsCount = 1; for (int col = 0; col < colscount; col++) { if (dgv.Columns[col].Visible == true) { try { objExcel.Cells[row + 2, displayColumnsCount] = dgv.Rows[row].Cells[col].Value.ToString().Trim(); displayColumnsCount++; } catch (Exception) { } } } } //隐藏进度条 //tempProgressBar.Visible = false; //保存文件 objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); } catch (Exception error) { MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } finally { //关闭Excel应用 if (objWorkbook != null) objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value); if (objExcel.Workbooks != null) objExcel.Workbooks.Close(); if (objExcel != null) objExcel.Quit(); objsheet = null; objWorkbook = null; objExcel = null; } MessageBox.Show(fileNameString + "\n\n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); } #endregion这里是事件 上面是写成的方法 你也可以成个类 试试看 private void 导出XLSToolStripMenuItem_Click(object sender, EventArgs e) { DataGridViewToExcel(dgvPLU); } 你的多维表头是怎么做的呀?我有个能导出到EXCEL的。不知道你原来的表头是如何生成的,所以搞不清楚这个方法对你有用没 wpf+listview可视的第一行 checkbox问题 vs2005以上的版本不支持RS485串口操作么? c#主线程如何知道子线程执行结束 实现将无驱摄像头的图像显示在winform界面 关于网站的文章内容如何保存? 在C#里,一个长字符串要分行写,请问怎么实现 C# 中关键字 yield 的问题,可能与枚举器有关.谢谢! 菜鸟求教;访问属性 c#初学, 无法安装vs.net ide环境?? 请问高手用C#给手机发短信 【求助】winform 中导出Excel(导出透视表,筛选数据)
{
try
{
string exfilename = gridtextname;
DialogResult rs = DialogResult.No;
SaveFileDialog saved = new SaveFileDialog();
saved.InitialDirectory = "d:\\";
saved.DefaultExt = "*.xls";
//saved.FileName = exfilename.Substring(0, exfilename.Length - 4);
saved.FileName = exfilename;
saved.Filter = "XLS Files|*.xls";
saved.FilterIndex = 2;
saved.RestoreDirectory = true; if (saved.ShowDialog() == DialogResult.OK)
{ exfilename = saved.FileName;
DateTime start = DateTime.Now;
exprot.Export(grid, exfilename);
TimeSpan timeSpan = DateTime.Now - start;
rs = MessageBox.Show("数据导出操作成功,打开文件吗? ", "提示信息 ", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (rs == DialogResult.Yes)
{
System.Diagnostics.ProcessStartInfo st = new System.Diagnostics.ProcessStartInfo(exfilename);
st.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
System.Diagnostics.Process.Start(st);
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
MessageBox.Show(ex.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// <summary>
/// 打开Excel并将DataGridView控件中数据导出到Excel
/// </summary>
/// <param name="dgv">DataGridView对象 </param>
/// <param name="isShowExcle">是否显示Excel界面 </param>
/// <res>
/// Excel数据导入方法
/// 作者:zxx
///时间:2011-5-30
/// add com "Microsoft Excel 11.0 Object Library"
/// using Excel=Microsoft.Office.Interop.Excel;
/// </res>
/// <returns> </returns>
public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle)
{
if (dgv.Rows.Count == 0)
return false;
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成字段名称
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < dgv.RowCount - 1; 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();
}
}
}
return true;
}
#endregion
#region DataGridView导出到Excel,有一定的判断性
/// <summary>
///方法,导出DataGridView中的数据到Excel文件
/// </summary>
/// <res>
/// /// Excel数据导入方法
/// 作者:zxx
/// 时间:2011-5-30
/// 添加 com "Microsoft Excel 11.0 Object Library"
/// 命名空间:using Excel=Microsoft.Office.Interop.Excel;
/// 命名空间:using System.Reflection;
/// </res>
/// <param name= "dgv"> DataGridView </param>
public static void DataGridViewToExcel(DataGridView dgv)
{
#region 验证可操作性 //申明保存对话框
SaveFileDialog dlg = new SaveFileDialog();
//默然文件后缀
dlg.DefaultExt = "xls ";
//文件后缀列表
dlg.Filter = "EXCEL文件(*.XLS)|*.xls ";
//默然路径是系统当前路径
dlg.InitialDirectory = Directory.GetCurrentDirectory();
//打开保存对话框
if (dlg.ShowDialog() == DialogResult.Cancel) return;
//返回文件路径
string fileNameString = dlg.FileName;
//验证strFileName是否为空或值无效
if (fileNameString.Trim() == " ")
{ return; }
//定义表格内数据的行数和列数
int rowscount = dgv.Rows.Count;
int colscount = dgv.Columns.Count;
//行数必须大于0
if (rowscount <= 0)
{
MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
} //列数必须大于0
if (colscount <= 0)
{
MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
} //行数不可以大于65536
if (rowscount > 65536)
{
MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
} //列数不可以大于255
if (colscount > 255)
{
MessageBox.Show("数据记录行数太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
} //验证以fileNameString命名的文件是否存在,如果存在删除它
FileInfo file = new FileInfo(fileNameString);
if (file.Exists)
{
try
{
file.Delete();
}
catch (Exception error)
{
MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
#endregion
Microsoft.Office.Interop.Excel.Application objExcel = null;
Microsoft.Office.Interop.Excel.Workbook objWorkbook = null;
Microsoft.Office.Interop.Excel.Worksheet objsheet = null;
try
{
//申明对象
objExcel = new Microsoft.Office.Interop.Excel.Application();
objWorkbook = objExcel.Workbooks.Add(Missing.Value);
objsheet = (Microsoft.Office.Interop.Excel.Worksheet)objWorkbook.ActiveSheet;
//设置EXCEL不可见
objExcel.Visible = false; //向Excel中写入表格的表头
int displayColumnsCount = 1;
for (int i = 0; i <= dgv.ColumnCount - 1; i++)
{
if (dgv.Columns[i].Visible == true)
{
objExcel.Cells[1, displayColumnsCount] = dgv.Columns[i].HeaderText.Trim();
displayColumnsCount++;
}
}
//设置进度条
//tempProgressBar.Refresh();
//tempProgressBar.Visible = true;
//tempProgressBar.Minimum = 1;
//tempProgressBar.Maximum = dgv.RowCount;
//tempProgressBar.Step = 1;
//向Excel中逐行逐列写入表格中的数据
for (int row = 0; row <= dgv.RowCount - 1; row++)
{
//tempProgressBar.PerformStep(); displayColumnsCount = 1;
for (int col = 0; col < colscount; col++)
{
if (dgv.Columns[col].Visible == true)
{
try
{
objExcel.Cells[row + 2, displayColumnsCount] = dgv.Rows[row].Cells[col].Value.ToString().Trim();
displayColumnsCount++;
}
catch (Exception)
{ } }
}
}
//隐藏进度条
//tempProgressBar.Visible = false;
//保存文件
objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
}
catch (Exception error)
{
MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
finally
{
//关闭Excel应用
if (objWorkbook != null) objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);
if (objExcel.Workbooks != null) objExcel.Workbooks.Close();
if (objExcel != null) objExcel.Quit(); objsheet = null;
objWorkbook = null;
objExcel = null;
}
MessageBox.Show(fileNameString + "\n\n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information); } #endregion这里是事件 上面是写成的方法 你也可以成个类 试试看
private void 导出XLSToolStripMenuItem_Click(object sender, EventArgs e)
{
DataGridViewToExcel(dgvPLU);
}