导出DataSet为Excel文件问题

西客小贝壳 2004-12-24 01:20:52
假如我有一个DataSet,里面有相当多的数据,
1.怎样对这些数据再筛选,比如使用Select语句检索?

2.我可以将它保存为XML文件,怎么将它保存为Excel文件呀?

请给出具体方法。
再次感谢

...全文
390 13 打赏 收藏 转发到动态 举报
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;

111,098

社区成员

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

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

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