将excel表格保存到我在本地机器中创建的特定文件夹中,并将此excel表格设为只读
本文关键字:表格 excel 文件夹 只读 创建 机器 保存 | 更新日期: 2023-09-27 17:58:46
在我的应用程序中,我正在将gridview数据导出到excel工作表中,现在我想将此工作表保存到我在机器中创建的文件夹中,我该如何进行
我写了这样的代码
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public override void VerifyRenderingInServerForm(Control control)
{
}
private void ExportToExcel(string strFileName, GridView dg)
{
Response.Clear();
Response.Buffer = true;
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);
oHtmlTextWriter.WriteLine("<b><u><font size='5'><font color='blue'><center> REPORT </center></font></u></b>");
GridView1.RenderControl(oHtmlTextWriter);
Response.End();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("");
con.Open();
int var = DropDownList1.SelectedIndex;
switch (var)
{
case 0: break;
case 1: SqlCommand cmdd = new SqlCommand("update t1 set diff1=isnull(t1.date-t2.date,0)from reporttemp t1 left join reporttemp t2 on t1.rn=t2.rn+1", con);
SqlCommand cmdd1 = new SqlCommand("update t1 set diff2=isnull(t1.date-t2.date,0)from reportpre t1 left join reportpre t2 on t1.rn=t2.rn+1", con);
SqlCommand cmdd2 = new SqlCommand("update t1 set diff3=isnull(t1.date-t2.date,0)from reportph t1 left join reportph t2 on t1.rn=t2.rn+1", con);
cmdd.ExecuteNonQuery();
cmdd1.ExecuteNonQuery();
cmdd2.ExecuteNonQuery();
GridView1.Visible = true;
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("select Date,Temperature from reporttemp where datepart(minute,diff1)=5", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlCommand cmd2 = new SqlCommand("select Date,Pressure from reportpre where datepart(minute,diff2)=5", con);
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
SqlCommand cmd3 = new SqlCommand("select Date,Ph from reportph where datepart(minute,diff3)=5", con);
SqlDataAdapter da3 = new SqlDataAdapter(cmd3);
da.Fill(dt);
da2.Fill(dt);
da3.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
break;
case 2: GridView1.Visible = true;
SqlCommand cmd4 = new SqlCommand("update t1 set diff1=isnull(t1.date-t2.date,0)from reporttemp t1 left join reporttemp t2 on t1.rn=t2.rn+1", con);
SqlCommand cmd1 = new SqlCommand("update t1 set diff2=isnull(t1.date-t2.date,0)from reportpre t1 left join reportpre t2 on t1.rn=t2.rn+1", con);
SqlCommand cmd5 = new SqlCommand("update t1 set diff3=isnull(t1.date-t2.date,0)from reportph t1 left join reportph t2 on t1.rn=t2.rn+1", con);
cmd4.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
cmd5.ExecuteNonQuery();
GridView1.Visible = true;
DataTable dt1 = new DataTable();
SqlCommand cmd6 = new SqlCommand("select Date,Temperature from reporttemp where datepart(minute,diff1)=2 ", con);
SqlDataAdapter daa = new SqlDataAdapter(cmd6);
SqlCommand cmd7 = new SqlCommand("select Date,Pressure from reportpre where datepart(minute,diff2)=2", con);
SqlDataAdapter daa2 = new SqlDataAdapter(cmd7);
SqlCommand cmd8 = new SqlCommand("select Date,Ph from reportph where datepart(minute,diff3)=2", con);
SqlDataAdapter daa3 = new SqlDataAdapter(cmd8);
daa.Fill(dt1);
daa2.Fill(dt1);
daa3.Fill(dt1);
GridView1.DataSource = dt1;
GridView1.DataBind();
break;
}
}
protected void Button3_Click1(object sender, EventArgs e)
{
ExportToExcel("Report.xls", GridView1);
}
}
有人能在这个上帮我吗
从这个问题来看,我从未想过这是一个web应用程序?!
private void ExportToExcel(string strFileName, GridView dg)
{
//..
string text = oStringWriter.ToString();
Response.Write(text);
// OR use Response.WriteBinary() to write a byte[] directly
当然,这是一个网络应用程序,浏览器在控制之中。这里的客户端正在加载文件;这取决于浏览器(和用户)来决定它存储在哪里,以及它是否可写。
如果您想要更多的控制,您需要查看(专有的)客户端框架,如Java Applets、Silverlight、Flex。
然而,您可能会重新考虑目的(为什么您想要拥有这种控制?您是否以错误的方式做事?)
首先使用StreamWriter编写流
FileStream FileStream=新的FileStream(@"Location+Filename.xls",FileMode.Create);
此外,如果你想将其保存在我的文档文件夹中,你必须使用
环境SpecialFolder。我的文档
有关此的更多信息,请单击
希望会有所帮助……:-)
请明确您要写的内容。ASP。NET应用程序是预期在用户机器的浏览器上运行的服务器应用程序。因此,服务器应用程序不能直接写入客户端的机器。浏览器将提示显示"保存文件"对话框。如果您希望创建一个只读文件的复杂性,您可以在服务器端创建该文件,使其只读,然后使其可供下载。
SaveFileDialog oDialog = new SaveFileDialog();
oDialog.Filter = "Excel files | *.xls";
if (oDialog.ShowDialog() == DialogResult.OK)
{
string sFileName = oDialog.FileName;
}
app = new Microsoft.Office.Interop.Excel.Application();
workbook = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
if (sFileName != null)
{
workbook.SaveAs(sFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
misValue,misValue, misValue, misValue, misValue);
workbook.Close(misValue, misValue, misValue);
app.Quit();
}
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText.ToUpper();
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value;
}
}