将数据表值存储在Excel工作表中,excel工作表应保存在特定驱动器上 [英] Store datatable value in excel sheet and that excel sheet should be save on perticular drive

查看:85
本文介绍了将数据表值存储在Excel工作表中,excel工作表应保存在特定驱动器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据表数据存储在Excel工作表上,这个excel工作表保存在我的PC的特定驱动器上,如D:



i必须执行以下代码,但excel表保存在我的电脑的 下载 文件夹中。



如何将它保存在D:?



我尝试过:



protected void btndatatabletoexcel_Click(object sender,EventArgs e )

{

objtestBO.Id = 1;

objtestBO.Name = txtnm.Text;

objtestBO。 City = txtcity.ToString();

int result = objtestBL.Insert_test(objtestBO);

if(result> 0)

{

u.MsgThenRedirectTo(objtestBO.Name +成功添加!!,datatabletoexcel);

DataTable dt = new DataTable();

dt = objtestBL.Selecttest();

UploadDataTableToExcel(dt);

}

}



protected void UploadDataTableToExcel(DataTable dtRecords)

{

string XlsPath = Server.MapPath(@ 〜/ add_data / test.xls);

string attachment = string.Empty;

if(XlsPath.IndexOf(\\)!= -1 )

{

string [] strFileName = XlsPath.Split(new char [] {'\\'});

附件=附件; filename =+ strFileName [strFileName.Length - 1];

}

else

attachment =attachment; filename =+ XlsPath;

try

{

Response.ClearContent();

Response.AddHeader( content-disposition,附件);

Response.ContentType =application / vnd.ms-excel;

string tab = string.Empty;



foreach(dtRecords.Columns中的DataColumn datacol)

{

Response.Write(tab + datacol.ColumnName);

tab =\t;

}

Response.Write(\ n);



foreach(ddRecords.Rows中的DataRow博士)

{

tab =;

for( int j = 0; j< dtRecords.Columns.Count; j ++)

{

Response.Write(tab + Convert.ToString(dr [j]));

tab =\t;

}



Response.Write(\ n);

}

Response.End() ;

}

catch(例外情况)

{

//Response.Write(ex.Message) ;

}

}

Store datatable data on excel sheet and this excel sheet save on particular drive of my PC like on D:

i have to do following code but the excel sheet save on Download folder of my PC.

How i would to save it on D: ?

What I have tried:

protected void btndatatabletoexcel_Click(object sender, EventArgs e)
{
objtestBO.Id = 1;
objtestBO.Name = txtnm.Text;
objtestBO.City = txtcity.ToString();
int result = objtestBL.Insert_test(objtestBO);
if (result > 0)
{
u.MsgThenRedirectTo(objtestBO.Name + " Added Successfully!!", "datatabletoexcel");
DataTable dt = new DataTable();
dt = objtestBL.Selecttest();
UploadDataTableToExcel(dt);
}
}

protected void UploadDataTableToExcel(DataTable dtRecords)
{
string XlsPath = Server.MapPath(@"~/Add_data/test.xls");
string attachment = string.Empty;
if (XlsPath.IndexOf("\\") != -1)
{
string[] strFileName = XlsPath.Split(new char[] { '\\' });
attachment = "attachment; filename=" + strFileName[strFileName.Length - 1];
}
else
attachment = "attachment; filename=" + XlsPath;
try
{
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = string.Empty;

foreach (DataColumn datacol in dtRecords.Columns)
{
Response.Write(tab + datacol.ColumnName);
tab = "\t";
}
Response.Write("\n");

foreach (DataRow dr in dtRecords.Rows)
{
tab = "";
for (int j = 0; j < dtRecords.Columns.Count; j++)
{
Response.Write(tab + Convert.ToString(dr[j]));
tab = "\t";
}

Response.Write("\n");
}
Response.End();
}
catch (Exception ex)
{
//Response.Write(ex.Message);
}
}

推荐答案

你无法将文件保存到客户端的硬盘上,可以吗?想象一下安全问题,如果可以的话?



用户可以自行决定保存文件的位置,如果保存的话。您无法控制它们的保存位置。
You can't save files onto the client's hard drive, can you imagine the security issues if you could?

It is up to the user to decide where the file is saved, if it is saved at all. You can't control where they are saved to either.


这篇关于将数据表值存储在Excel工作表中,excel工作表应保存在特定驱动器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆