mathieu_cupryk
asked on
1000 pts datagrid to excel with columns readonly.
I have written the program and still i can modify the columns in excel.
Here is my aspx code:
=============
<%@ Page language="c#" Codebehind="WebForm3.aspx. cs" AutoEventWireup="false" Inherits="PDM.excel.WebFor m3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScri pt">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body text="black" bgColor="silver" MS_POSITIONING="GridLayout ">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 72px" runat="server"
GridLines="Vertical" AutoGenerateColumns="False " CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None"
BorderColor="#E7E7FF">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></Sele ctedItemSt yle>
<AlternatingItemStyle BackColor="#F7F7F7"></Alte rnatingIte mStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></Item Style>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></Head erStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></Foot erStyle>
<Columns>
<asp:BoundColumn DataField="string_id" ReadOnly="true" HeaderText="String ID"></asp:BoundColumn>
<asp:BoundColumn DataField="string" ReadOnly="true" HeaderText="String"></asp: BoundColum n>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></Page rStyle>
</asp:datagrid><asp:dropdo wnlist id="Language_DropDownList" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 24px"
runat="server" AutoPostBack="True"></asp: dropdownli st><asp:la bel id="Label1" style="Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 24px" runat="server"
Width="248px" Font-Bold="True">Choose Your Language To Convert:</asp:label><asp:b utton id="Button1" style="Z-INDEX: 104; LEFT: 392px; POSITION: absolute; TOP: 24px" runat="server"
Text="Submit"></asp:button ></form>
</body>
</HTML>
and asp.cs
======
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls ;
using System.Data.SqlClient;
using System.Xml.Xsl;
using System.Xml;
namespace PDM.excel
{
/// <summary>
/// Summary description for WebForm3.
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls. DataGrid DataGrid1;
public SqlConnection conDB = new SqlConnection();
static public string[] LanguageStr =
{
"English","French","Spanis h"};
static public string[] DropDownListStr =
{
"Language_DropDownList"};
protected System.Web.UI.WebControls. DropDownLi st Language_DropDownList;
protected System.Web.UI.WebControls. Label Label1;
protected System.Web.UI.WebControls. Button Button1;
static public string[][] AddStrArray =
{
LanguageStr};
private void Fill_DropDownList()
{
for (int i = 0; i < DropDownListStr.Length; i++)
{
string CtrlName= DropDownListStr[i];
DropDownList MyList = (DropDownList)Page.FindCon trol(CtrlN ame);
// Populate all the checkboxes
string[] ToPopulate = AddStrArray[i];
for (int j = 0; j < ToPopulate.Length; j++)
{
MyList.Items.Add(new ListItem(ToPopulate[j], j.ToString()));
}
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
Fill_DropDownList();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Language_DropDownList .SelectedI ndexChange d += new System.EventHandler(this.L anguage_Dr opDownList _SelectedI ndexChange d);
this.Button1.Click += new System.EventHandler(this.B utton1_Cli ck);
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
public DataSet DataToExcel = new DataSet();
public void BindGrid (int index)
{
conDB.ConnectionString = "data source=10.195.17.7;databas e=devnew;u id=bounaaj ak;pwd=ehs first;pack et size=4096";
SqlDataAdapter da = new SqlDataAdapter("Select string_id, string from pdm_translations where language_id = 1 and string_id not in (select string_id from pdm_translations where language_id = " + index.ToString() + ") ", conDB);
da.Fill(DataToExcel, "DataToExcel");
Session["Tabla"] = Language_DropDownList.Sele ctedItem.T ext;
DataToExcel.WriteXml(Serve r.MapPath( Session["T abla"] + ".xml"));
DataGrid1.DataSource = DataToExcel;
DataGrid1.DataBind();
// Get row count
int nRowCount = DataToExcel.Tables["DataTo Excel"].Ro ws.Count;
// Store Row count in Session variable
Session["RowCount"] = nRowCount;
}
private void Language_DropDownList_Sele ctedIndexC hanged(obj ect sender, System.EventArgs e)
{
int getSelectedIndex = Language_DropDownList.Sele ctedIndex+ 1;
BindGrid(getSelectedIndex) ;
}
public void DataGridToExcel(DataGrid Export,HttpResponse Response)
{
// Set the Response mime type for excel
Response.ContentType = "application/vnd.ms-excel" ;
// Create a String Writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWrit er hw = new System.Web.UI.HtmlTextWrit er(tw);
DataGrid1.RenderControl(hw );
Response.Write(tw.ToString ());
Response.End();
}
private void Button1_Click(object sender, System.EventArgs e)
{
DataGridToExcel (DataGrid1, Response);
}
}
}
Here is my aspx code:
=============
<%@ Page language="c#" Codebehind="WebForm3.aspx.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScri
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body text="black" bgColor="silver" MS_POSITIONING="GridLayout
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 72px" runat="server"
GridLines="Vertical" AutoGenerateColumns="False
BorderColor="#E7E7FF">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></Sele
<AlternatingItemStyle BackColor="#F7F7F7"></Alte
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></Item
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></Head
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></Foot
<Columns>
<asp:BoundColumn DataField="string_id" ReadOnly="true" HeaderText="String ID"></asp:BoundColumn>
<asp:BoundColumn DataField="string" ReadOnly="true" HeaderText="String"></asp:
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></Page
</asp:datagrid><asp:dropdo
runat="server" AutoPostBack="True"></asp:
Width="248px" Font-Bold="True">Choose Your Language To Convert:</asp:label><asp:b
Text="Submit"></asp:button
</body>
</HTML>
and asp.cs
======
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
using System.Data.SqlClient;
using System.Xml.Xsl;
using System.Xml;
namespace PDM.excel
{
/// <summary>
/// Summary description for WebForm3.
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.
public SqlConnection conDB = new SqlConnection();
static public string[] LanguageStr =
{
"English","French","Spanis
static public string[] DropDownListStr =
{
"Language_DropDownList"};
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
static public string[][] AddStrArray =
{
LanguageStr};
private void Fill_DropDownList()
{
for (int i = 0; i < DropDownListStr.Length; i++)
{
string CtrlName= DropDownListStr[i];
DropDownList MyList = (DropDownList)Page.FindCon
// Populate all the checkboxes
string[] ToPopulate = AddStrArray[i];
for (int j = 0; j < ToPopulate.Length; j++)
{
MyList.Items.Add(new ListItem(ToPopulate[j], j.ToString()));
}
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
Fill_DropDownList();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Language_DropDownList
this.Button1.Click += new System.EventHandler(this.B
this.Load += new System.EventHandler(this.P
}
#endregion
public DataSet DataToExcel = new DataSet();
public void BindGrid (int index)
{
conDB.ConnectionString = "data source=10.195.17.7;databas
SqlDataAdapter da = new SqlDataAdapter("Select string_id, string from pdm_translations where language_id = 1 and string_id not in (select string_id from pdm_translations where language_id = " + index.ToString() + ") ", conDB);
da.Fill(DataToExcel, "DataToExcel");
Session["Tabla"] = Language_DropDownList.Sele
DataToExcel.WriteXml(Serve
DataGrid1.DataSource = DataToExcel;
DataGrid1.DataBind();
// Get row count
int nRowCount = DataToExcel.Tables["DataTo
// Store Row count in Session variable
Session["RowCount"] = nRowCount;
}
private void Language_DropDownList_Sele
{
int getSelectedIndex = Language_DropDownList.Sele
BindGrid(getSelectedIndex)
}
public void DataGridToExcel(DataGrid Export,HttpResponse Response)
{
// Set the Response mime type for excel
Response.ContentType = "application/vnd.ms-excel"
// Create a String Writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWrit
DataGrid1.RenderControl(hw
Response.Write(tw.ToString
Response.End();
}
private void Button1_Click(object sender, System.EventArgs e)
{
DataGridToExcel (DataGrid1, Response);
}
}
}
may be you can make the whole excel file readonly...
ASKER
Well If I can make the fields all readonly from datagrid. Then by all means But I have been stuck on this problem for days.
Let me investigate a little but I think You cant modify some setting of Excel from ASP.NET
You should Insted Use Visual Studio Tools for Office. Install it and give it a try.
I will confirm later if this can be donde from asp.net
Or you can try something different.
like genereate the FIle on the Server. and then with System.IO objects change the file to readonly attributes.??
You should Insted Use Visual Studio Tools for Office. Install it and give it a try.
I will confirm later if this can be donde from asp.net
Or you can try something different.
like genereate the FIle on the Server. and then with System.IO objects change the file to readonly attributes.??
ASKER
The thing is with using the Excel object I have to set impersonate=true
I am not aloud to do that for this project.:-(
I am not aloud to do that for this project.:-(
why? you have to think in something different for the security? Please tell us why would u use it.
I am still confirming my investigation
I am still confirming my investigation
ASKER
I am not aloud the company will not allow me.
ASKER
I cannot use a super user or with attribute impersonate = true.
The reason for the column to be read only. Is so that some does not modify it accidently.
The reason for the column to be read only. Is so that some does not modify it accidently.
but when you generate an excel file It Generates ON the CLIENT MACHINE! if a second User generates It Wont see any changes from the first user.
Whats your application sceneario?
Whats your application sceneario?
ASKER
It will be generated on the client machine and the client will tranlate in the third column field the desired languange. Only one person will be doing the translation. Who I don't know. This is my situation.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I must add a header and make the datagrid invisible but have the save come up?