谁能给个用C#从SQL 2005中导出数据到excel文档的例子?非常感谢!

mikyy 2010-03-17 02:44:41
C#最好做成个应用程序,有专门的窗口。
...全文
102 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mikyy 2010-03-26
  • 打赏
  • 举报
回复
谢谢,我先试试。
hengsf2008 2010-03-17
  • 打赏
  • 举报
回复
winform?先将数据库导入DataSet,在DataGridView显示,然后再导入Excel就可以了

private void insertToExcel(DataGridView gridView)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = false; //文件不存在是是否提示新建
saveFileDialog.OverwritePrompt = false; //文件存在是是否提示覆盖
saveFileDialog.Title = "文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != 0)
{
if (strName != FileName)
{
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true); ;
excel.Visible = false; //若是true,则在导出的时候会显示EXcel界面。
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "Sheet1";

for (int i = 0; i < gridView.ColumnCount; i++) //生成字段名称
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText.ToString();
}

for (int i = 0; i < gridView.RowCount; i++) //填充数据
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].Value == typeof(string))
{
excel.Cells[i + 2, j + 1] = "" + gridView[i, j].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
}
sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "追加完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
KillExcel();
}
else
{
MessageBox.Show("该文件正在被使用,请用别的文件名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.dgvshow.DataSource = dt;
}
}
}
fzxu_05 2010-03-17
  • 打赏
  • 举报
回复
我用控制台的你要不要?
Sakeyi 2010-03-17
  • 打赏
  • 举报
回复
都是附带在项目里,,哪里有专门做个窗口的,,要时间地,,你能读2005的数据,,再搜下怎么新建EXCEL工作簿就行啦,基本写进去非常容易,只要找到那几个新建工作簿的命令就行了
jackcong2 2010-03-17
  • 打赏
  • 举报
回复
新建一个页面,在页面上输入你要生成的东西。在.cs页面中的PAGE_LOAD事件里加上这句话。:
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("统计表", System.Text.Encoding.UTF8) + ".xls");

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧