帮帮初学者,求导出EXCEL的代码 我要将SQL中数据导成EXCEL,哪位大侠给我段代码参考啊?谢谢了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般放在DGV中这样比较好,再从DGV中取出下面是从DGV全部取出后保存 string saveFileName = ""; //bool fileSaved = false; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xls"; saveDialog.Filter = "Excel文件|*.xls"; saveDialog.FileName = "成绩册" + DateTime.Now.ToShortDateString(); saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; 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 = 1; r < dataGridView1.Rows.Count - 1; r++) { for (int i = 0; i < dataGridView1.ColumnCount; i++) { string str = dataGridView1.Rows[r].Cells[i].Value.ToString(); if (string.IsNullOrEmpty(str)) { str = " "; } worksheet.Cells[r + 1, i + 1] = str; } System.Windows.Forms.Application.DoEvents(); } worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 //if (Microsoft.Office.Interop.cmbxType.Text != "Notification") //{ // Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[ds.Tables[0].Rows.Count + 1, 2]); // rg.NumberFormat = "00000000"; //} if (saveFileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(saveFileName); //fileSaved = true; } catch (Exception ex) { //fileSaved = false; MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message); } } //else //{ // fileSaved = false; //} xlApp.Quit(); GC.Collect();//强行销毁 MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK); 再发个选择导出 你注意下表头 DataTable dt = new DataTable(); DataTable dt2 = new DataTable(); for (int i = 0; i < dataGridView1.ColumnCount; i++) { if (i == 0) { dt.Columns.Add("准考证号", typeof(System.String)); dt2.Columns.Add("准考证号", typeof(System.String)); } else { dt.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String)); dt2.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String)); } } /////这是打印分数条的格式 DataRow dr; for (int r = 0; r < dataGridView1.SelectedRows.Count; r++) { dr = dt.NewRow(); for (int i = 0; i < dataGridView1.ColumnCount; i++) { dr[i] = dataGridView1.SelectedRows[r].Cells[i].Value.ToString(); } dt.Rows.Add(dr); } DataRow dr2; for (int i = dt.Rows.Count - 1; i >= 0; i--) { dr2 = dt.NewRow(); for (int j = 0; j < dt.Columns.Count; j++) { string ss = dt.Rows[i][j].ToString(); dr2[j] = dt.Rows[i][j]; } dt2.Rows.Add(dr2.ItemArray);//不加ItemArray报错 } //放到G2中再放入EXCEL // MessageBox.Show(dataGridView1.CurrentCell.RowIndex.ToString()); DataTable dts = new DataTable(); Excel.Application excel = new Excel.Application(); excel.Application.Workbooks.Add(true); //// excel.Columns.EntireColumn.AutoFit();// 列宽自适应 excel.Visible = true; //生成字段名称 for (int i = 0; i < dataGridView1.ColumnCount; i++) { excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; } for (int i = 0; i < dataGridView1.ColumnCount; i++) { if (i == 0) { dts.Columns.Add("准考证号", typeof(System.String)); } else { dts.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String)); } } for (int i = 0; i < dt2.Rows.Count; i++) { for (int j = 0; j < dt2.Columns.Count; j++) { excel.Cells[2 + i, 1 + j] = dt2.Rows[i][j].ToString(); } } excel.Columns.EntireColumn.AutoFit();//写在前面还有点问题 http://blog.csdn.net/lsj_zrp/archive/2009/07/29/4390944.aspx C#启动外部程序,但不显示其界面 C#向ACCESS中插入数据的自增列问题 请教一个关于socket的问题 如何将一组图片添加进listbox??????? 如何实现自动登陆功能? 如何取得精确到毫秒的系统时间? 请教:DevExpress的XtraTreeList控件大量数据刷新问题! 类型转换 对一个表生成多个视图,总是得到最后的视图。 怎样得到和设置一个已经打开的Excel文件中的单元格的值 C# 写了一个对话框程序 读取sql2005 中文乱码? 结贴率100%~~~一个关于tabControl的问题???
string saveFileName = "";
//bool fileSaved = false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = "成绩册" + DateTime.Now.ToShortDateString();
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
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 = 1; r < dataGridView1.Rows.Count - 1; r++)
{
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{ string str = dataGridView1.Rows[r].Cells[i].Value.ToString();
if (string.IsNullOrEmpty(str))
{
str = " ";
} worksheet.Cells[r + 1, i + 1] = str;
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 //if (Microsoft.Office.Interop.cmbxType.Text != "Notification")
//{
// Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[ds.Tables[0].Rows.Count + 1, 2]);
// rg.NumberFormat = "00000000";
//} if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
//fileSaved = true;
}
catch (Exception ex)
{
//fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
} }
//else
//{
// fileSaved = false;
//}
xlApp.Quit();
GC.Collect();//强行销毁 MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK);
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i == 0)
{
dt.Columns.Add("准考证号", typeof(System.String));
dt2.Columns.Add("准考证号", typeof(System.String)); }
else
{
dt.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String));
dt2.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String));
}
} /////这是打印分数条的格式
DataRow dr;
for (int r = 0; r < dataGridView1.SelectedRows.Count; r++)
{
dr = dt.NewRow();
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
dr[i] = dataGridView1.SelectedRows[r].Cells[i].Value.ToString();
}
dt.Rows.Add(dr);
}
DataRow dr2;
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
dr2 = dt.NewRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
string ss = dt.Rows[i][j].ToString();
dr2[j] = dt.Rows[i][j]; }
dt2.Rows.Add(dr2.ItemArray);//不加ItemArray报错
}
//放到G2中再放入EXCEL
// MessageBox.Show(dataGridView1.CurrentCell.RowIndex.ToString());
DataTable dts = new DataTable();
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
//// excel.Columns.EntireColumn.AutoFit();// 列宽自适应
excel.Visible = true;
//生成字段名称
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
} for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i == 0)
{
dts.Columns.Add("准考证号", typeof(System.String)); }
else
{
dts.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String));
}
} for (int i = 0; i < dt2.Rows.Count; i++)
{ for (int j = 0; j < dt2.Columns.Count; j++)
{
excel.Cells[2 + i, 1 + j] = dt2.Rows[i][j].ToString();
}
} excel.Columns.EntireColumn.AutoFit();//写在前面还有点问题