社区
C#
帖子详情
导出DataSet为Excel文件问题
西客小贝壳
2004-12-24 01:20:52
假如我有一个DataSet,里面有相当多的数据,
1.怎样对这些数据再筛选,比如使用Select语句检索?
2.我可以将它保存为XML文件,怎么将它保存为Excel文件呀?
请给出具体方法。
再次感谢
...全文
393
13
打赏
收藏
导出DataSet为Excel文件问题
假如我有一个DataSet,里面有相当多的数据, 1.怎样对这些数据再筛选,比如使用Select语句检索? 2.我可以将它保存为XML文件,怎么将它保存为Excel文件呀? 请给出具体方法。 再次感谢
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
13 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
lcj_abc
2005-04-14
打赏
举报
回复
顶一下
西客小贝壳
2005-01-27
打赏
举报
回复
不想用com,怕有局限性,有什么更好的解决方法吗?
up
fifadeke
2005-01-27
打赏
举报
回复
/// <summary>
/// 向EXCEL表格里插入数据库记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExcel_Click(object sender, System.EventArgs e)
{
//查询数据库内容判断 AccFlage的值,如果其值是1则用'√'替换,如果是0则为空
string query_sql = "select AccDate,No,Abr_AccId,Abr_Content,Abr_Person,(select case when AccFlag = 1 then '√' when AccFlag = 0 then ''else ''end as 'AccFlag'),DbtAmt,CrdAmt,EndAmt from accounttype where accno = '" + cboNo.SelectedValue + "'and AccDate between '" + dateStart.Value.Date + "' and '" + dateEnd.Value.Date + "'";
SqlConnection con = new SqlConnection(ConStr);
SqlDataAdapter da = new SqlDataAdapter(query_sql,con);
DataSet ds = new DataSet();
da.Fill(ds,"accounttype");
string query = "select endamt from accounttype where id = '" + this.txtDate.Text + "'";
SqlDataAdapter daNow = new SqlDataAdapter(query,con);
DataSet dsNow = new DataSet();
daNow.Fill(dsNow,"accounttype");
txtNow.DataBindings.Clear();
txtNow.DataBindings.Add("Text",dsNow,"AccountType.EndAmt");
string str;
if(this.radioMoney.Checked)
{
str = "0";
}
else
{
str = "1";
}
if(str == "0")
{
string query_money = "select * from money where MoneyAttr = 2";
SqlDataAdapter damoney = new SqlDataAdapter(query_money,con);
DataSet dsmoney = new DataSet();
damoney.Fill(dsmoney,"money");
txtMoney.DataBindings.Clear();
txtMoney.DataBindings.Add("Text",dsmoney,"Money.Money");
}
else
{
string query_money = "select * from money where MoneyAttr = 3";
SqlDataAdapter damoney = new SqlDataAdapter(query_money,con);
DataSet dsmoney = new DataSet();
damoney.Fill(dsmoney,"money");
txtMoney.DataBindings.Clear();
txtMoney.DataBindings.Add("Text",dsmoney,"Money.Money");
}
string filename="";
//将模板文件复制到一个新文件中
SaveFileDialog mySave = new SaveFileDialog();
mySave.Filter="Excel文件(*.XLS)|*.xls|所有文件(*.*)|*.*";
if(mySave.ShowDialog()!=DialogResult.OK)
{
return;
}
else
{
filename = mySave.FileName;
//将模板文件copy到新位置,建议实际开发时用相对路径,如Application.StartupPath.Trim()+"\\report\\normal.xls"
FileInfo mode=new FileInfo(Application.StartupPath.Trim() + @"\Report\CashReport.xls");
try
{
mode.CopyTo(filename,true);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
//打开复制后的文件
object missing = Missing.Value;
Excel.Application myExcel = new Excel.Application ( );
//打开新文件
myExcel.Application.Workbooks.Open(filename,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);
//将Excel显示出来
myExcel.Visible=true;
//将列标题和实际内容选中
Excel.Workbook myBook = myExcel.Workbooks[1];
Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1];
mySheet.Cells[2,5] = this.dateStart.Value.Date;
mySheet.Cells[2,8] = this.dateEnd.Value.Date;
mySheet.Cells[1,1] = "安徽省建行现金出纳统计表------" + this.cboNo.SelectedValue;
//判断是否有余额,如果没有就把初期额添加入EXCEL中
if(this.txtDate.Text == "")
{
mySheet.Cells[5,9] = txtMoney.Text.ToString();
}
else
{
mySheet.Cells[5,9] = txtNow.Text.ToString();
}
//向EXCEL里插记录
int HeadLines=5;
int j=0;
for(int r = 0;r<ds.Tables[0].Rows.Count;r++)
{
if (((r+1) % 22)==0)
{
for(int i = 0;i<ds.Tables[0].Columns.Count;i++)
{
mySheet.Cells[j*2+r+HeadLines+1,i+1] = ds.Tables[0].Rows[r][i];
}
mySheet.Cells[j*2+r+HeadLines+2,4] = "过次页";
mySheet.Cells[j*2+r+HeadLines+3,4] = "呈上页";
mySheet.Cells[j*2+r+HeadLines+3,9] = ds.Tables[0].Rows[r][8];;
j++;
}
else
{
for(int i = 0;i<ds.Tables[0].Columns.Count;i++)
{
mySheet.Cells[j*2+r+HeadLines+1,i+1] = ds.Tables[0].Rows[r][i];
}
}
}
}
洪十二
2005-01-07
打赏
举报
回复
WebForm:
Response.Clear();
Response.Charset = "big5";
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter oSW = new System.IO.StringWriter();
HtmlTextWriter oHW = new HtmlTextWriter(oSW);
DataGrid oDG = new DataGrid();
oDG.DataSource = oDS.Tables[0];
oDG.DataBind();
oDG.RenderControl(oHW);
Response.Write(oSW.ToString());
Response.Flush();
Response.Close();
yanglg
2005-01-07
打赏
举报
回复
那是因为myExcel.Cells[j,k]的j,k不能从0开始,是从1开始,正确的写法应该是
myExcel.Cells[j+1,k+1]=ds.Tables["Customers"].Rows[j][k].ToString();
试试吧
不过,这种方法太慢了,我也在找一种比较快的方法
luckyanglg@hotmail.com
西客小贝壳
2005-01-07
打赏
举报
回复
up
西客小贝壳
2005-01-06
打赏
举报
回复
myExcel.Cells[j,k]=ds.Tables["Customers"].Rows[j][k].ToString();//这条语句出错了
myExcel.Cells是从1开始的,不是0。
up,问题还没有解决哟,不想用com,怕有局限性,有什么更好的解决方法吗?
比如Excel11.0的com对office2000就不能很好的支持
nga96
2004-12-25
打赏
举报
回复
dr=dsPatient.Tables[0.Select("你的条件语句");
可以这样写么?明天测试一下
西客小贝壳
2004-12-25
打赏
举报
回复
提示:HRESULT 中的异常:0x800A03EC。
西客小贝壳
2004-12-25
打赏
举报
回复
为什么我的代码不行?
运行后数据在dategrid中显示出来了,但是用到Excel时出错?
引用的是Excel10.0的com
using Excel;
...
private void button1_Click(object sender, System.EventArgs e)
{
//string s="select top 100 * from Customers";
DataSet ds =new DataSet();
//DataRow[] dr ;//=new DataRow();
sqlDataAdapter1.Fill(ds,"Customers");
dataGrid1.DataSource=ds.Tables["Customers"].DefaultView;
dataGrid1.SetDataBinding(ds,"Customers");
/*DataRow[] dr;
try
{
dr=ds.Tables["Customers"].DefaultView;
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
}*/
try //在这里出错了
{
Excel.Application myExcel=new Excel.Application();
myExcel.Application.Workbooks.Add(true);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet mySheet=(Excel.Worksheet)myBook.Worksheets[1];
//dr=ds.Tables["Customers"];
for(int j=0;j<ds.Tables["Customers"].Rows.Count;j++)
{
for(int k=0;k<ds.Tables["Customers"].Columns.Count;k++)
{
myExcel.Cells[j,k]=ds.Tables["Customers"].Rows[j][k].ToString();
}
}
myExcel.Visible=true;
}
catch(Exception ex)
{
textBox1.Text=ex.Message;
}
}
西客小贝壳
2004-12-24
打赏
举报
回复
请问引用是怎么引用的?
它支持Office 2000吗?
Cry_Out
2004-12-24
打赏
举报
回复
除了楼上的逐格写的方法,还有没有更快的将dataset写入excel的方法呢?
lyvvvv
2004-12-24
打赏
举报
回复
1。
DataRow[] dr;
try
{
dr=dsPatient.Tables[0.Select("你的条件语句");
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
return;
}
2。
Excel.Application myExcel=new Excel.Application();
myExcel.Application.Workbooks.Add(true);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet mySheet=(Excel.Worksheet)myBook.Worksheets[1];
for(j=0;j<dr.Length;j++)
{
for(k=0;k<ds.Tables[0].Columns.Count;k++)
{
myExcel.Cells[j,k]=dr[j][k] .ToString();
}
}
myExcel.Visible=true;
C#
导出
DataSet
到
EXCEL
首先,让我们讨论如何
导出
DataSet
到
Excel
文件
。在C#中,我们可以利用Microsoft Office Interop库来操作
Excel
,但这需要在运行环境中有安装Office。以下是一个基本的步骤: 1. 引用所需的库:在项目中添加对`...
.net(
dataset
)输出流
导出
excel
.net(
dataset
)输出流
导出
excel
(无需生成模版
excel
文件
,直接输出数据流
导出
excel
表格)
C#
DataSet
导出
EXCEL
的方法
C#
DataSet
导出
EXCEL
的方法是指使用C#语言将
DataSet
中的数据
导出
到
Excel
文件
中的方法。在本文中,我们将介绍两种不同的方法来实现
DataSet
到
Excel
的
导出
。 描述解释 从描述中可以看出,这篇文章主要是为了介绍C#...
导入
导出
EXCEL
导出
Excel
导出
带线的
Excel
导入
DataSet
首先,"导入
导出
EXCEL
"是指将数据库或其他数据源的数据写入
Excel
文件
,或从
Excel
文件
中读取数据并加载到程序的数据结构,如
DataSet
。在.NET中,通常使用Microsoft.Office.Interop.
Excel
库(需要安装Office)或者开源...
NPOI 包解决.net
DataSet
导出
excel
问题
在.NET环境中,如果你需要将
DataSet
对象的数据
导出
到
Excel
文件
,NPOI提供了高效且灵活的解决方案。
DataSet
是.NET Framework中的一个数据结构,它能存储来自多种数据源的数据,并提供了类似数据库的功能,如查询和...
C#
111,112
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章