请问C#中如何将数据集中的数据存为EXCEL文件?.xls,.dbf等格式的

xiaoshancool 2007-04-19 05:00:44
我将数据库中的表读到DataSet中,如何把它保存为.xls,.dbf等格式的文件?请教了,谢谢!!!
...全文
736 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
请叫我低调 2008-05-28
  • 打赏
  • 举报
回复
楼上的不错 不过再改进下 应该会跟好
优途科技 2008-05-23
  • 打赏
  • 举报
回复
           try
{
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();//创建Excel表
myExcel.Visible = true;
Microsoft.Office.Interop.Excel.Workbooks myWorkbooks = myExcel.Workbooks;//创建Excel工作表
Microsoft.Office.Interop.Excel.Workbook myWorkbook = myWorkbooks.Add(System.Reflection.Missing.Value); //创建Excel工作表
Microsoft.Office.Interop.Excel.Worksheet myWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkbook.Worksheets[1];//创建Excel工作表Sheet1页
Microsoft.Office.Interop.Excel.Range myrange = myWorksheet.get_Range("A1", "D1");//选择页的范围,从“A1”开始,我这是四列所以到“D1”

object[] myhead = { "日期", "组名", "付款人", "金额" };//设置表头
myrange.Value2 = myhead;//表头的值传入Excel表

if (dgvRecord.Rows.Count > 0)
{
myrange = myWorksheet.get_Range("A2", System.Reflection.Missing.Value);
int row = 0;
row = dgvRecord.Rows.Count;//设置表的行数
int col = 4;//设置表的列数
object[,] mydata = new object[row, col];
for (int i = 0; i < row; i++)//循环读取DataGridView上的数据
{
for (int j = 0; j < col; j++)
{
mydata[i, j] = dgvRecord[j, i].Value.ToString();//读取DataGridView上的值保存到mydata数组中,这里要注意哦,DataGridView是列在前,而Excel刚好相反
}
}
myrange = myrange.get_Resize(row, col);
myrange.Value2 = mydata;//把madata数组的值存到Excel表导出
myrange.EntireColumn.AutoFit();
}
myExcel = null;//清空表单
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
zhysino 2008-05-15
  • 打赏
  • 举报
回复
不过打不开那个页,郁闷.
zhysino 2008-05-15
  • 打赏
  • 举报
回复
谢谢五楼,正在参考中
epngllh 2007-04-19
  • 打赏
  • 举报
回复
楼上的是在web下的代码
win下的代码可以参考如下网址:
http://www.it130.net/Csharp/WinForm-dataGridView-Excel-2176.htm
xiaoshancool 2007-04-19
  • 打赏
  • 举报
回复
我只想在.net环境中点击按钮然后出来个保存对话框,然后输入要保存的文件名,确定即可
具体后台代码怎么写?谢谢
confei 2007-04-19
  • 打赏
  • 举报
回复
解决方案:
页面增加一个按钮,单击事件添加如下方法:
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
}

private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
还有由于是文件操作所以要引入名称空间IO和Text

后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
public partial class Default7 : System.Web.UI.Page
{
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=sa";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();

}
}

public void bind()
{
string sqlstr = "select top 5 * from 飞狐工作室";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "飞狐工作室");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "身份证号码" };
GridView1.DataBind();
sqlcon.Close();
}

protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "学生成绩报表.xls");
}

private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}

}

前台:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" >
<FooterStyle BackColor="White" ForeColor="#000066" />
<Columns>
<asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" />
<asp:BoundField DataField="姓名" HeaderText="姓名" />
<asp:BoundField DataField="出生日期" HeaderText="邮政编码" />
<asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
<asp:BoundField DataField="邮政编码" HeaderText="邮政编码" />
<asp:BoundField DataField="起薪" HeaderText="起薪" />

</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/>
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出" />
xiaoshancool 2007-04-19
  • 打赏
  • 举报
回复
具体怎么实现?
出来个另存为对话框,输入要保存的文件名...
是不是从引用中加入某个东西,还是细说以下的好
在那边上网,今天做车来市里上网,好辛苦!谢谢了!
LoveCleverDog 2007-04-19
  • 打赏
  • 举报
回复
excel类, 直接向excel 文件中写

110,534

社区成员

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

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

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