如何使用C#来生成execl文件?

stt_19850602 2005-11-23 12:48:12
如何使用C#来生成execl文件?就是在点击一个按钮后就可以实现EXECL文件的生成啊!!
...全文
135 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dong_king 2005-11-23
  • 打赏
  • 举报
回复
我只知道windows的
昨天刚学的,这种问题自己先搜索以下
1添加com组件 microsoft excel10.0 object library
2 using Excel
3 public bool TransToExcel(string storeprocedureName)
{
try
{
Excel.Application myExcel=new ApplicationClass();
object filename=System.IO.Directory.GetCurrentDirectory();
System.DateTime.Now.ToFileTime().ToString()+".xls";
object nothing=System.Reflection.Missing.Value ;
myExcel.Workbooks.Add(nothing);
SqlServer ss=new SqlServer();
DataSet ds=ss.ExcelDataSet(storeprocedureName);
for(int i=0;i<ds.Tables[0].Columns.Count;i++)
{
myExcel.Cells[1,i+1]=ds.Tables[0].Columns[i].ColumnName; }
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
for(int j=0;j<ds.Tables[0].Columns.Count;j++)
{
myExcel.Cells[i+2,j+1]=ds.Tables[0].Rows[i][j].ToString();
}
}
myExcel.ActiveWorkbook._SaveAs(filename,nothing,nothing,nothing,nothing,nothing,XlSaveAsAccessMode.xlExclusive,nothing,nothing,nothing,nothing);
myExcel.Visible=true;
return true;
}
catch(Exception ee)
{
MessageBox.Show(ee.ToString());return false;
}
mamoon 2005-11-23
  • 打赏
  • 举报
回复
楼主没有说是从哪里获取的数据源呀
从不签到 2005-11-23
  • 打赏
  • 举报
回复
Dai ma ru xia :
private void btnExcel_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(DataGrid1);
DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}


private void ClearControls(Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
} return;
}
shopvictor 2005-11-23
  • 打赏
  • 举报
回复
生成csv多好用 而且快捷 excel也可以直接打开

110,571

社区成员

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

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

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