各位大哥,小弟我遇到这样一个问题,在一个Web页面中有2个DataGrid,分别显示两张的数据集,现在怎么把它导出到一个Excel文件中
解决方案 »
- 我想知道那个 很久很久以前发生的 js 与 WebService 的故事
- Repeater控件中ItemType值总是默认等于Header(设断点查到的),只有去掉前台的Header模块才能执行下面代码
- 自定义类中的时间问题
- 将数据导出到Excel
- datagrid中列是自动产生的,如何让列的宽度固定下来
- 怎么像url重写一样,换个别的顶级域名呢?
- treeview递归绑定数据库字段到树中
- ThoughtWorks.QRCode.dll 中文生成二维码问题
- 请问哪位知道如何通过代码将现有的.aspx及其.vb文件加入到项目中?
- 进行数据库操作,除了使用控件以外,还要使用诸如sqlconnection这些类吗?
- 编程方式如何使用FreeTextBox???恭送100分!!!
- 怎么让光标在打开页面的时候就定位在一个Textbox上面
SqlDataReader dr_GetMyAllMsg = null;
produce MyPro=new produce();
dr_GetMyAllMsg=MyPro.GetMyAllMsg();
DataTable dt=WebApplication13.myComponents.Tools.ConvertDataReaderToDataTable(dr_GetMyAllMsg);
string name="c://"+DateTime.Today.ToString("yyyyMMdd")+new Random(DateTime.Now.Millisecond).Next(10000).ToString()+".csv";//存放到web.config中downloadurl指定的路径,文件格式为当前日期+4位随机数
FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
sw.WriteLine("产品编号,产品名称,规格,单位,销售参考价格,警报数量,备注");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["pro_num"]+","+dr["pro_name"]+","+dr["pro_KG"]+","+dr["pro_unite"]+","+dr["pro_price"]+","+dr["pro_warmnum"]+","+dr["pro_re"]);
}
sw.Close();
//fs.Close(); Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
Response.ContentType = "application/ms-excel";
Response.WriteFile(name);
第二DataGrid的字段也是这3个如果这样的话实现起来应该比较简单
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders= "", ls_item="";
int i=0; //定义表对象与行对像,同时用DataSet对其值进行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select("");
// typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
if(typeid=="1")
{
resp.ContentType = "application/ms-excel";
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for(i=0;i<dt.Columns.Count-1;i++)
{
colHeaders+=dt.Columns[i].Caption.ToString()+"\t";
}
colHeaders +=dt.Columns[i].Caption.ToString() +"\n";
//向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
//逐行处理数据
foreach(DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
for(i=0;i<dt.Columns.Count-1;i++)
{
if(row[i].ToString().Equals(null))
{
ls_item += ""+"\t";
}
else
{
ls_item +=row[i].ToString() + "\t";
}
}
ls_item += row[i].ToString() +"\n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
ls_item="";
}
}
else
{
if(typeid=="2")
{
//从DataSet中直接导出XML数据并且写到HTTP输出流中
resp.Write(ds.GetXml());
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();
}
public void ClearControls(Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}
#endregion #region 汇出资料方法
private void Data_Excel(Control control)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="UTF-8"; Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
ClearControls(control); control.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End();
}
#endregionprivate void Button1_Click(object sender, System.EventArgs e)
{
Data_Excel(dg_VABorrow_Dtl);
}
#endregion
for(int i=0;i<myset.Tables[0].Columns.Count;i++)
{
mywrite.Write(myset.Tables[0].Columns[i].Caption+ " ");
}
mywrite.WriteLine();for (int j = 0 ; j< myset.Tables[0].Rows.Count;j++)
{
for (int k =0; k< myset.Tables[0].Columns.Count;k++)
{
mywrite.Write(myset.Tables[0].Rows[j].ItemArray.GetValue(k) + " ");
}
mywrite.WriteLine();
}
mywrite.WriteLine();mywrite.Close();
MessageBox.Show("网格中数据已被写入到文件夹bin\\Debug下的"+"aa.xls"+"文件中...","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);