c#怎么把gridControl中的数据导出到excel中呀?? 5
2个回答
展开全部
public void WriteToExcel(DataTable table,string fileName)
{
try
{
//string tempImagePath = Application.StartupPath;//软件安装目录
//string temp = tempImagePath + "\\Execl";//目录下的Execl文件夹
//Directory.CreateDirectory(@temp);
string strFilePath = fileName; //赋给文件的名字
System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, System.Text.Encoding.Default); //写入流
object[] values = new object[table.Columns.Count];
for (int i = 0; i < table.Columns.Count; ++i)
{
if (table.Columns[i].Caption.ToString() == "id")
{
table.Columns[i].Caption = "编号";
}
else if (table.Columns[i].Caption.ToString() == "inmoney")
{
table.Columns[i].Caption = "金额";
}
else if (table.Columns[i].Caption.ToString() == "totalMoney")
{
table.Columns[i].Caption = "剩余金额";
}
else if (table.Columns[i].Caption.ToString() == "createTime")
{
table.Columns[i].Caption = "创建时间";
}
else if (table.Columns[i].Caption.ToString() == "description")
{
table.Columns[i].Caption = "描述";
}
else if (table.Columns[i].Caption.ToString() == "typeName")
{
table.Columns[i].Caption = "类型";
}
sw.Write(table.Columns[i].Caption.ToString());
sw.Write('\t');
}
sw.Write("\r\n");
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < values.Length; ++j)
{
sw.Write(table.Rows[i][j]);
sw.Write('\t');
}
sw.Write("\r\n");
}
sw.Flush();
sw.Close();
MessageBox.Show("成功导出[" + table.Rows.Count.ToString() + "]行到Execl!");
}
catch
{
MessageBox.Show("导出Execl失败!");
}
}
{
try
{
//string tempImagePath = Application.StartupPath;//软件安装目录
//string temp = tempImagePath + "\\Execl";//目录下的Execl文件夹
//Directory.CreateDirectory(@temp);
string strFilePath = fileName; //赋给文件的名字
System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, System.Text.Encoding.Default); //写入流
object[] values = new object[table.Columns.Count];
for (int i = 0; i < table.Columns.Count; ++i)
{
if (table.Columns[i].Caption.ToString() == "id")
{
table.Columns[i].Caption = "编号";
}
else if (table.Columns[i].Caption.ToString() == "inmoney")
{
table.Columns[i].Caption = "金额";
}
else if (table.Columns[i].Caption.ToString() == "totalMoney")
{
table.Columns[i].Caption = "剩余金额";
}
else if (table.Columns[i].Caption.ToString() == "createTime")
{
table.Columns[i].Caption = "创建时间";
}
else if (table.Columns[i].Caption.ToString() == "description")
{
table.Columns[i].Caption = "描述";
}
else if (table.Columns[i].Caption.ToString() == "typeName")
{
table.Columns[i].Caption = "类型";
}
sw.Write(table.Columns[i].Caption.ToString());
sw.Write('\t');
}
sw.Write("\r\n");
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < values.Length; ++j)
{
sw.Write(table.Rows[i][j]);
sw.Write('\t');
}
sw.Write("\r\n");
}
sw.Flush();
sw.Close();
MessageBox.Show("成功导出[" + table.Rows.Count.ToString() + "]行到Execl!");
}
catch
{
MessageBox.Show("导出Execl失败!");
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是form还是web呀,web的话简单的就是
//Excel
Response.AppendHeader("Content-Disposition", "attachment;filename=temp.xls");
Response.ContentType = "application/ms-excel";
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
this.pnlPrinter.RenderControl(htmlWriter);
//输出
Response.Write(writer.ToString());
Response.End();
//Excel
Response.AppendHeader("Content-Disposition", "attachment;filename=temp.xls");
Response.ContentType = "application/ms-excel";
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
this.pnlPrinter.RenderControl(htmlWriter);
//输出
Response.Write(writer.ToString());
Response.End();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询