How to trans data from datagridview to excel? How to trans data from datagridview to excel 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ASP.Net 2.0: Export GridView to Excel http://www.cnblogs.com/sparon/archive/2007/04/06/702946.html public bool exportToExcel(DataGridView dataGridView1, string Title) { System.Data.DataTable myTable = (System.Data.DataTable)dataGridView1.DataSource; try { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); int rowIndex; int colIndex; rowIndex = 2; colIndex = 0; Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true); if (dataGridView1.Rows.Count > 0) { Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, dataGridView1.Columns.Count]); range.MergeCells = true; xlApp.ActiveCell.FormulaR1C1 = Title; xlApp.ActiveCell.Font.Size = 18; xlApp.ActiveCell.Font.Bold = true; foreach (DataGridViewColumn colu in dataGridView1.Columns) { colIndex = colIndex + 1; xlApp.Cells[2, colIndex] = colu.HeaderText; } //得到的表所有行,赋值给单元格 for (int row = 0; row < myTable.Rows.Count; row++) { rowIndex = rowIndex + 1; colIndex = 0; for (int col = 0; col < dataGridView1.Columns.Count; col++) { colIndex = colIndex + 1; xlApp.Cells[rowIndex, colIndex] = dataGridView1.Rows[row].Cells[col].Value; } } } else { Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myTable.Columns.Count]); range.MergeCells = true; xlApp.ActiveCell.FormulaR1C1 = Title; xlApp.ActiveCell.Font.Size = 18; xlApp.ActiveCell.Font.Bold = true; //将表中的栏位名称填到Excel的第一行 foreach (DataColumn Col in myTable.Columns) { colIndex = colIndex + 1; xlApp.Cells[2, colIndex] = Col.ColumnName; } //得到的表所有行,赋值给单元格 for (int row = 0; row < myTable.Rows.Count; row++) { rowIndex = rowIndex + 1; colIndex = 0; for (int col = 0; col < myTable.Columns.Count; col++) { colIndex = colIndex + 1; xlApp.Cells[rowIndex, colIndex] = dataGridView1.Rows[row].Cells[col].Value; } } } xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true; xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1; xlApp.Cells.EntireColumn.AutoFit(); xlApp.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter; xlApp.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter; xlApp.Visible = true; } catch (Exception ex) { throw ex; } return true; } http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx 求助 if循环跳过 C# 读取txt文档抓取数据问题 webpart 和 用户控件 问题 HttpwebRequest的问题,大家来看看 先要做一个网络广告系统,如何实现多个广告(文字、图片或视频),按各自的播放时长(譬如说都播放5分钟)轮换播放? 悬赏缉拿! ASP.NET自定义控件问题 资源的问题 c#windows应有程序中,实现一个左右移动的圆 并在园内显示圆心的坐标 暑假想帮家里货物公司做一个软件打单的,已经有excel模板了,怎么用C#做一个winform输入后打印? 怎么在数据库里删除listBox包含的全部内容 菜牛求助如何将byte[] 另外复制一份?
{
System.Data.DataTable myTable = (System.Data.DataTable)dataGridView1.DataSource; try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); int rowIndex;
int colIndex; rowIndex = 2;
colIndex = 0; Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true); if (dataGridView1.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, dataGridView1.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = Title;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true; foreach (DataGridViewColumn colu in dataGridView1.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = colu.HeaderText;
} //得到的表所有行,赋值给单元格 for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < dataGridView1.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = dataGridView1.Rows[row].Cells[col].Value;
}
}
}
else
{
Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myTable.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = Title;
xlApp.ActiveCell.Font.Size = 18;
xlApp.ActiveCell.Font.Bold = true; //将表中的栏位名称填到Excel的第一行 foreach (DataColumn Col in myTable.Columns)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = Col.ColumnName;
} //得到的表所有行,赋值给单元格 for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myTable.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = dataGridView1.Rows[row].Cells[col].Value;
}
}
}
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true;
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1; xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlApp.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlApp.Visible = true;
}
catch (Exception ex)
{
throw ex;
}
return true;
}