c#怎么把gridControl中的数据导出到excel中呀??
答案:2 悬赏:40 手机版
解决时间 2021-04-19 12:49
- 提问者网友:寂寞梧桐
- 2021-04-18 14:25
c#怎么把gridControl中的数据导出到excel中呀??
最佳答案
- 五星知识达人网友:低血压的长颈鹿
- 2021-04-18 14:44
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失败!");
}
}
全部回答
- 1楼网友:往事隔山水
- 2021-04-18 15:44
你是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();
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯