如何在c#中从网格视图创建一个受传递保护的Excel文件 [英] How to create a pass protected excel file from grid view in c#

查看:88
本文介绍了如何在c#中从网格视图创建一个受传递保护的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,

我想从网格视图创建excel文件。但我想要一个额外的功能,那就是crested文件应该受密码保护。是否可以先从网格视图创建受密码保护的文件,以便excel。

解决方案

是的!有可能

您可能会看到此链接供您参考



http://forums.asp.net/t/1184531.aspx [ ^ ]



http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx [ ^ ]

您好,

我为你发布了代码,为gridview生成excel文件。我不知道有没有办法在生成时为密码保护excel文件。尽管我给它一个提示。使用XSLT可以为gridview生成excel文件,可以选择在XSLT中保护excel文件。您可以在XSLT文件中提供格式。试试这个提示。



这是gridview的XSLT示例文件格式。

 <   xsl:  style      version   =  1.0    xmlns   =  urn:schemas-microsoft-com:office:spreadsheet    xmlns:xsl   =  http:/ /www.w3.org/1999/XSL/Transform\">  
的xmlns:msxsl =瓮:架构 - 微软-COM: xsltxmlns:user =urn:my-scriptsxmlns:o =urn:schemas-microsoft-com:office:office
xmlns:x = urn:schemas-microsoft-com:office:excelxmlns:ss =urn:schemas-microsoft-com:office:spreadsheet>
< xsl:template 匹配 = NewPatients >
< workbook < span class =code-attribute> xmlns = urn:schemas-microsoft-com:office:spreadsheet xmlns:o = urn:schemas-microsoft-com:office:office >
的xmlns:X = 瓮:架构 - 微软-COM:办公室:EXCEL的xmlns:SS = 瓮:架构 - 微软-COM:办公室:电子表格
xmlns:html =http://www.w3.org/TR/REC-html40>
< documentproperties < span class =code-attribute> xmlns
= urn:schemas-microsoft-com:office: office > < / documentproperties >
< excelworkbook xmlns = urn:schemas-microsoft-com:office:excel > < / excelworkbook >
< 风格 s >
< style ss:id =Headerxmlns:ss =#unknown>
< alignment ss:horizo​​ntal =Centerss:vertical =Bottomss:wraptext =1/>
<跨度类= 代码leadattribute><字体 SS:字体名= Arial字体 ×:家族= 瑞士 □□:粗体= 1 的xmlns:X = #未知 />
< / style >
< style ss:id = HeaderTop xmlns:ss = #unknown >
< alignment ss:horizo​​ntal =Leftss:vertical =Topss:wraptext =0/>
< font ss:fontname =Arialx:family =Swissss:bold =1xmlns:x =#unknown />
< / style >
< style ss:id = HeaderYellow xmlns:ss = #unknown >
< alignment ss:horizo​​ntal =Centerss:vertical =Bottomss:wraptext =1/>
< borders>
< border ss:position =Bottomss:linestyle =Continuousss:weight =1/>
< border ss:position =Leftss:linestyle =Continuousss:weight =1/>
< border ss:position =rightss:linestyle =Continuousss:weight =1/>
< border ss:position =Topss:linestyle =Continuousss:weight =1/>
< / border >
< font ss:fontname = MS Sans Serif x:family = < span class =code-keyword> Swiss ss:color = #FF0000 ss:粗体 = 1 xmlns:x = #unknown / >
< interior ss:color = #FFFF00 ss:pattern = 实体 / >
< / style >
< style ss:id = 百分比 xmlns:ss = #未知 >
< numberformat ss:format =0 \\ n \\%/>
< / style >
< style ss:id = HeaderStyle xmlns:ss = #unknown >
< alignment ss:horizo​​ntal =Centerss:vertical =Centerss:wraptext =1/>
< borders>
< border ss:position =Bottomss:linestyle =Continuousss:weight =1/>
< border ss:position =Topss:linestyle =Continuousss:weight =1/>
< / border >
< font ss:fontname = Arial x:family = Swiss ss:size = 9 ss:color = #000000 ss:bold = 1 xmlns:x = #unknown / >
< interior ss:color = #D8D8D8 ss:pattern = 实体 / >
< numberformat ss:format = @ / >
< < span class =code-keyword> /
style >

< style ss:id = StringDataStyle xmlns:ss = #unknown >
< alignment ss: horizo​​ntal =Leftss:vertical =Topss:wraptext =1/>
< borders>
< border ss:position =Bottomss:linestyle =Continuousss:weight =1/>
< border ss:position =Topss:linestyle =Continuousss:weight =1/>
< / border >
< font ss:fontname = Arial x:family = Swiss ss:size = 9 ss:color = #000000 xmlns:x = #unknown / > ;
< numberformat ss:format = @ / >
< / style >
< style < span class =code-attribute> ss:id = SpecificStringDataStyle xmlns:ss = #unknown >
< alignment ss:horizo​​ntal =Rightss:vertical =Topss:wraptext =1 />
< borders>
< border ss:position =Bottomss:linestyle =Continuousss:weight =1/>
< border ss:position =Topss:linestyle =Continuousss:weight =1/>
< / border >
< font ss:fontname = Arial x:family = Swiss ss:size = 9 ss:color = #000000 xmlns:x = #unknown / > ;
< numberformat ss:format = @ / >
< / style >
< / < span class =code-leadattribute> style s >
< span class =code-keyword>< 工作表 ss:name = 新患者 xmlns:ss = #unknown >
< table xsl:version = 1.0 < span class =code-attribute> xmlns:xsl = http://www.w3。 org / 1999 / XSL / Transform >
< column ss:autofitwid th = 0 ss:width = 90 / >
< span class =code-keyword>< column ss:autofitwidth = 0 ss:width = 110 / >
< ss:autofitwidth = 0 ss:width = 110 / >
< ss:autofitwidth = 0 ss:width = 70 / >
< ss:autofitwidth = 0 ss:width = 90 / >
< < span class =code-leadattribute> column ss:autofitwidth = 0 ss:width = 130 / >
< ss:autofitwidth = 0 ss:width = 90 / >
< ss:autofitwidth = 0 ss:width = 90 / >
< ss:autofitwidth = 0 ss:width = 100 / < span class =code-keyword>>
< ss:autofitwidth = 0 ss:width = 100 / >
< < span class =code-leadattribute> row >
< cell ss: style id = HeaderTop >
< data ss:type = 字符串 > InfuScience - 临床进度跟踪< / data >
< / cell >
< / row >
< row >
< cell ss: style < span class =code-attribute> id = HeaderTop >
< data ss:type = 字符串 > 新患者< span class =code-keyword>< / data >
< / cell >
< / row >
< >
< cell ss: style id = HeaderStyle >
< data ss:type = < span class =code-keyword>字符串 > MRN < / data >
< / cell >
< cell ss: 样式 id = HeaderStyle >
< data ss:type = 字符串 > 名字< / data >
< / cell >
< cell ss: style id = HeaderStyle >
< 数据 ss:type = 字符串 > 姓氏< ; / data >
< / cell >
< cell ss: 样式 id = HeaderStyle >
< 数据 ss:type = String > 治疗< / data >
< / cell < span class=\"code-keyword\">>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Sales Rep.</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Physician</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Status</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Start Date</data >
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Tracking Count</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Tracked Count</data>
</cell>
</row>
<xsl:for-each select=\"Patient\">
<row>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"MRN\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"FirstName\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"LastName\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"Therapy\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"SalesRep\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"Physician\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"Status\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"StartDate\" />
</data>
</cell>
<cell ss:styleid=\"SpecificStringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"TrackingCount\" />
</data>
</cell>
<cell ss:styleid=\"SpecificStringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"TrackedCount\" />
</data>
</cell>
</row>
</xsl:for-each>

</table>
<worksheetoptions xmlns=\"urn:schemas-microsoft-com:office:excel\">
<selected
/>
<freezepanes />
<frozennosplit />
<splithorizontal>3</splithorizontal>
<toprowbottompane>3</toprowbottompane>
<activepane>2</activepane>
<panes>
<pane>
<number>3</number>
</pane>
<pane>
<number>2</number>
<activerow>2</activerow>
</pane>
</panes& gt;
<protectobjects>False</protectobjects>
<protectscenarios>False</protectscenarios>
</worksheetoptions>
</worksheet>
</workbook> $ b$b </xsl:template>
</xsl:stylesheet>





and this is for read XSLT file and transform to excel,



public void ExportNewPatientsToExcel() 
{
logger.Info(\"New Patients :: export to excel\");
string fileDirectory = string.Empty;

if (Session[Constants.SESSION_FILE_DIRECTORY] != null)
fileDirectory = Session[Constants.SESSION_FILE_DIRECTORY].ToString();
else
{
logger.Error(\"New Patients::File Cache folder is not set.\");
Response.Redirect(Constants.PAGE_ERROR);
}

HttpContext context = HttpContext.Current;

try
{
string xsltFileName = Context.Server.MapPath(Constants.NEW_PATIENTS_XSLT_FILE_NAME);
PatientCollection patientCollection = PatientBAO.GetNewPatients(ShowAllPatient);

if (patientCollection.Count > 0 && patientCollection != null)
{
string fileName = PatientBAO.GenerateNewPatientsAsExcel(fileDirectory, xsltFileName, patientCollection);
logger.Info(\"New Patients Excel version saved name :\" + fileName);
string fileNamePart = fileName.Substring(fileName.LastIndexOf(\"\\\") + 1);
fileNamePart = fileNamePart.Substring(fileNamePart.IndexOf(\"_\") + 1);//added to remove session id from file name

context.Items.Add(Constants.ENABLE_CACHE_SZ, Constants.ENABLE_CACHE);
context.Response.ClearContent();
context.Response.AddHeader(\"Content-Disposition\", \"attachment;filename=\" + fileNamePart);
context.Response.ContentType = \"application/octet-stream\";
context.Response.TransmitFile(fileName);
}
else
{
ShowPopUp(Resources.Patient.RecordNotFoundToExportExcel);
logger.Error(\"New patients data not found for export to excel.\");
}
}
catch (Exception exc)
{
logger.ErrorException(\"Error occured while export patient details to excel.\", exc);
}
finally
{
//HttpContext.Current.ApplicationInstance.CompleteRequest();
context.Response.End();
}
}


Hi,

I posted code for you, generate excel file for gridview. I dont know is there is a way to give password for protect a excel file, while generating. Eventhough i give a tip for it. Using XSLT you can generated excel file for a gridview, may have option for protect a excel file in XSLT. You can provide format in XSLT file. Try this tip.



Download source



This is XSLT sample file format for a gridview.

<xsl:stylesheet version=\"1.0\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"> 
\txmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" xmlns:user=\"urn:my-scripts\" xmlns:o=\"urn:schemas-micro soft-com:office:office\"
\txmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">
<xsl:template match=\"NewPatients\">
<workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">
\t\t\txmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
\t\t\txmlns:html=\"http://www.w3.org/TR/REC-html40\">
<documentproperties xmlns=\"urn:schemas-microsoft-com:office:office\"></documentproperties>
<excelworkbook xmlns=\"urn:schemas-microsoft-com:office:excel\"></excelworkbook>
<styles>
<style ss:id=\"Header\" xmlns:ss=\"#unknown\">
<alignment ss:horizontal=\"Center\" ss:vertical=\"Bottom\" ss:wraptext=\"1\" />
<font ss:fontname=\"Arial\" x:family=\"Swiss\" ss:bold=\"1\" xmlns:x=\"#unknown\" />
</style>
<style ss:id=\"HeaderTop\" xmlns:ss=\"#unknown\">
<alignment ss:horizontal=\"Left\" ss:vertical=\"Top\" ss:wraptext=\"0\" />
<font ss:fontname=\"Arial\" x:family=\"Swiss\" ss:bold=\"1\" xmlns:x=\"#unknown\" />
</style>
<style ss:id=\"HeaderYellow\" xmlns:ss=\"#unknown\">
<alignment ss:horizontal=\"Center\" ss:vertical=\"Bottom\" ss:wraptext=\"1\" />
<borders>
<border ss:position=\"Bottom\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
<border ss:position=\"Left\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
<border ss:position=\"Right\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
<border ss:position=\"Top\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
</borders>
<font ss:fontname=\"MS Sans Serif\" x:family=\"Swiss\" ss:color=\"#FF0000\" ss:bold=\"1\" xmlns:x=\"#unknown\" />
<interior ss:color=\"#FFFF00\" ss:pattern=\"Solid\" />
</style>
<style ss:id=\"Percentage\" xmlns:ss=\"#unknown\">
<numberformat ss:format=\"0\%\" />
</style>
<style ss:id=\"HeaderStyle\" xmlns:ss=\"#unknown\">
<alignment ss:horizontal=\"Center\" ss:vertical=\"Center\" ss:wraptext=\"1\" />
<borders>
<border ss:position=\"Bottom\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
<border ss:position=\"Top\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
</borders>
<font ss:fontname=\"Arial\" x:family=\"Swiss\" ss:size=\"9\" ss:color=\"#000000\" ss:bold=\"1\" xmlns:x=\"#unknown\" />
<interior ss:color=\"#D8D8D8\" ss:pattern=\"Solid\" />
<numberformat ss:format=\"@\" />
</style>

<style ss:id=\"StringDataStyle\" xmlns:ss=\"#unknown\">
<alignment ss:horizontal=\"Left\" ss:vertical=\"Top\" ss:wraptext=\"1\" />
<borders>
<border ss:position=\"Bottom\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
<border ss:position=\"Top\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
</borders>
<font ss:fontname=\"Arial\" x:family=\"Swiss\" ss:size=\"9\" ss:color=\"#000000\" xmlns:x=\"#unknown\" />
<numberformat ss:format=\"@\" />
</style>
<style ss:id=\"SpecificStringDataStyle\" xmlns:ss=\"#unknown\">
<alignment ss:horizontal=\"Right\" ss:vertical=\"Top\" ss:wraptext=\"1\" />
<borders>
<border ss:position=\"Bottom\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
<border ss:position=\"Top\" ss:linestyle=\"Continuous\" ss:weight=\"1\" />
</borders>
<font ss:fontname=\"Arial\" x:family=\"Swiss\" ss:size=\"9\" ss:color=\"#000000\" xmlns:x=\"#unknown\" />
<numberformat ss:format=\"@\" />
</style>
</styles>
<worksheet ss:name=\"New Patients\" xmlns:ss=\"#unknown\">
<table xsl:version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
<column ss:autofitwid th=\"0\" ss:width=\"90\" />
<column ss:autofitwidth=\"0\" ss:width=\"110\" />
<column ss:autofitwidth=\"0\" ss:width=\"110\" />
<column ss:autofitwidth=\"0\" ss:width=\"70\" />
<column ss:autofitwidth=\"0\" ss:width=\"90\" />
<column ss:autofitwidth=\"0\" ss:width=\"130\" />
<column ss:autofitwidth=\"0\" ss:width=\"90\" />
<column ss:autofitwidth=\"0\" ss:width=\"90\" />
<column ss:autofitwidth=\"0\" ss:width=\"100\" />
<column ss:autofitwidth=\"0\" ss:width=\"100\" />
<row>
<cell ss:styleid=\"HeaderTop\">
<data ss:type=\"String\">InfuScience - Clinical Progress Tracking</data>
</cell>
</row>
<row>
<cell ss:styleid=\"HeaderTop\">
<data ss:type=\"
String\">New Patients</data>
</cell>
</row>
<row>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">MRN</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">First Name</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Last Name</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Therapy</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Sales Rep.</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Physician</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Status</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Start Date</data >
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Tracking Count</data>
</cell>
<cell ss:styleid=\"HeaderStyle\">
<data ss:type=\"String\">Tracked Count</data>
</cell>
</row>
<xsl:for-each select=\"Patient\">
<row>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"MRN\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"FirstName\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"LastName\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"Therapy\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"SalesRep\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"Physician\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"Status\" />
</data>
</cell>
<cell ss:styleid=\"StringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"StartDate\" />
</data>
</cell>
<cell ss:styleid=\"SpecificStringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"TrackingCount\" />
</data>
</cell>
<cell ss:styleid=\"SpecificStringDataStyle\">
<data ss:type=\"String\">
<xsl:value-of select=\"TrackedCount\" />
</data>
</cell>
</row>
</xsl:for-each>

</table>
<worksheetoptions xmlns=\"urn:schemas-microsoft-com:office:excel\">
<selected
/>
<freezepanes />
<frozennosplit />
<splithorizontal>3</splithorizontal>
<toprowbottompane>3</toprowbottompane>
<activepane>2</activepane>
<panes>
<pane>
<number>3</number>
</pane>
<pane>
<number>2</number>
<activerow>2</activerow>
</pane>
</panes& gt;
<protectobjects>False</protectobjects>
<protectscenarios>False</protectscenarios>
</worksheetoptions>
</worksheet>
</workbook> $ b$b </xsl:template>
</xsl:stylesheet>





and this is for read XSLT file and transform to excel,



public void ExportNewPatientsToExcel() 
{
logger.Info(\"New Patients :: export to excel\");
string fileDirectory = string.Empty;

if (Session[Constants.SESSION_FILE_DIRECTORY] != null)
fileDirectory = Session[Constants.SESSION_FILE_DIRECTORY].ToString();
else
{
logger.Error(\"New Patients::File Cache folder is not set.\");
Response.Redirect(Constants.PAGE_ERROR);
}

HttpContext context = HttpContext.Current;

try
{
string xsltFileName = Context.Server.MapPath(Constants.NEW_PATIENTS_XSLT_FILE_NAME);
PatientCollection patientCollection = PatientBAO.GetNewPatients(ShowAllPatient);

if (patientCollection.Count > 0 && patientCollection != null)
{
string fileName = PatientBAO.GenerateNewPatientsAsExcel(fileDirectory, xsltFileName, patientCollection);
logger.Info(\"New Patients Excel version saved name :\" + fileName);
string fileNamePart = fileName.Substring(fileName.LastIndexOf(\"\\\") + 1);
fileNamePart = fileNamePart.Substring(fileNamePart.IndexOf(\"_\") + 1);//added to remove session id from file name

context.Items.Add(Constants.ENABLE_CACHE_SZ, Constants.ENABLE_CACHE);
context.Response.ClearContent();
context.Response.AddHeader(\"Content-Disposition\", \"attachment;filename=\" + fileNamePart);
context.Response.ContentType = \"application/octet-stream\";
context.Response.TransmitFile(fileName);
}
else
{
ShowPopUp(Resources.Patient.RecordNotFoundToExportExcel);
logger.Error(\"New patients data not found for export to excel.\");
}
}
catch (Exception exc)
{
logger.ErrorException(\"Error occured while export patient details to excel.\", exc);
}
finally
{
//HttpContext.Current.ApplicationInstance.CompleteRequest();
context.Response.End();
}
}


Sir,
I want to create excel file from grid view. But i want an additional feature in that is the crested file should be password protected. Is it possible sir to create a password protected file from grid view to excel.

解决方案

Yes! It is possible
you may see this link for your reference

http://forums.asp.net/t/1184531.aspx[^]

http://www.c-sharpcorner.com/UploadFile/DipalChoksi/exportxl_asp2_dc11032006003657AM/exportxl_asp2_dc.aspx[^]


Hi,
I posted code for you, generate excel file for gridview. I dont know is there is a way to give password for protect a excel file, while generating. Eventhough i give a tip for it. Using XSLT you can generated excel file for a gridview, may have option for protect a excel file in XSLT. You can provide format in XSLT file. Try this tip.

This is XSLT sample file format for a gridview.

<xsl:stylesheet version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts" xmlns:o="urn:schemas-microsoft-com:office:office"
	xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  <xsl:template match="NewPatients">
    <workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office">
			xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
			xmlns:html="http://www.w3.org/TR/REC-html40">
      <documentproperties xmlns="urn:schemas-microsoft-com:office:office"></documentproperties>
      <excelworkbook xmlns="urn:schemas-microsoft-com:office:excel"></excelworkbook>
      <styles>
        <style ss:id="Header" xmlns:ss="#unknown">
          <alignment ss:horizontal="Center" ss:vertical="Bottom" ss:wraptext="1" />
          <font ss:fontname="Arial" x:family="Swiss" ss:bold="1" xmlns:x="#unknown" />
        </style>
        <style ss:id="HeaderTop" xmlns:ss="#unknown">
          <alignment ss:horizontal="Left" ss:vertical="Top" ss:wraptext="0" />
          <font ss:fontname="Arial" x:family="Swiss" ss:bold="1" xmlns:x="#unknown" />
        </style>
        <style ss:id="HeaderYellow" xmlns:ss="#unknown">
          <alignment ss:horizontal="Center" ss:vertical="Bottom" ss:wraptext="1" />
          <borders>
            <border ss:position="Bottom" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Left" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Right" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Top" ss:linestyle="Continuous" ss:weight="1" />
          </borders>
          <font ss:fontname="MS Sans Serif" x:family="Swiss" ss:color="#FF0000" ss:bold="1" xmlns:x="#unknown" />
          <interior ss:color="#FFFF00" ss:pattern="Solid" />
        </style>
        <style ss:id="Percentage" xmlns:ss="#unknown">
          <numberformat ss:format="0\%" />
        </style>
        <style ss:id="HeaderStyle" xmlns:ss="#unknown">
          <alignment ss:horizontal="Center" ss:vertical="Center" ss:wraptext="1" />
          <borders>
            <border ss:position="Bottom" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Top" ss:linestyle="Continuous" ss:weight="1" />
          </borders>
          <font ss:fontname="Arial" x:family="Swiss" ss:size="9" ss:color="#000000" ss:bold="1" xmlns:x="#unknown" />
          <interior ss:color="#D8D8D8" ss:pattern="Solid" />
          <numberformat ss:format="@" />
        </style>

        <style ss:id="StringDataStyle" xmlns:ss="#unknown">
          <alignment ss:horizontal="Left" ss:vertical="Top" ss:wraptext="1" />
          <borders>
            <border ss:position="Bottom" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Top" ss:linestyle="Continuous" ss:weight="1" />
          </borders>
          <font ss:fontname="Arial" x:family="Swiss" ss:size="9" ss:color="#000000" xmlns:x="#unknown" />
          <numberformat ss:format="@" />
        </style>
        <style ss:id="SpecificStringDataStyle" xmlns:ss="#unknown">
          <alignment ss:horizontal="Right" ss:vertical="Top" ss:wraptext="1" />
          <borders>
            <border ss:position="Bottom" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Top" ss:linestyle="Continuous" ss:weight="1" />
          </borders>
          <font ss:fontname="Arial" x:family="Swiss" ss:size="9" ss:color="#000000" xmlns:x="#unknown" />
          <numberformat ss:format="@" />
        </style>
      </styles>
      <worksheet ss:name="New Patients" xmlns:ss="#unknown">
        <table xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <column ss:autofitwidth="0" ss:width="90" />
          <column ss:autofitwidth="0" ss:width="110" />
          <column ss:autofitwidth="0" ss:width="110" />
          <column ss:autofitwidth="0" ss:width="70" />
          <column ss:autofitwidth="0" ss:width="90" />
          <column ss:autofitwidth="0" ss:width="130" />
          <column ss:autofitwidth="0" ss:width="90" />
          <column ss:autofitwidth="0" ss:width="90" />
          <column ss:autofitwidth="0" ss:width="100" />
          <column ss:autofitwidth="0" ss:width="100" />
          <row>
            <cell ss:styleid="HeaderTop">
              <data ss:type="String">InfuScience - Clinical Progress Tracking</data>
            </cell>
          </row>
          <row>
            <cell ss:styleid="HeaderTop">
              <data ss:type="String">New Patients</data>
            </cell>
          </row>         
          <row>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">MRN</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">First Name</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Last Name</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Therapy</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Sales Rep.</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Physician</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Status</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Start Date</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Tracking Count</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Tracked Count</data>
            </cell>
          </row>
          <xsl:for-each select="Patient">
            <row>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="MRN" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="FirstName" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="LastName" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="Therapy" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="SalesRep" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="Physician" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="Status" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="StartDate" />
                </data>
              </cell>
              <cell ss:styleid="SpecificStringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="TrackingCount" />
                </data>
              </cell>
              <cell ss:styleid="SpecificStringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="TrackedCount" />
                </data>
              </cell>              
            </row>
          </xsl:for-each>

        </table>
        <worksheetoptions xmlns="urn:schemas-microsoft-com:office:excel">
          <selected />
          <freezepanes />
          <frozennosplit />
          <splithorizontal>3</splithorizontal>
          <toprowbottompane>3</toprowbottompane>
          <activepane>2</activepane>
          <panes>
            <pane>
              <number>3</number>
            </pane>
            <pane>
              <number>2</number>
              <activerow>2</activerow>
            </pane>
          </panes>
          <protectobjects>False</protectobjects>
          <protectscenarios>False</protectscenarios>
        </worksheetoptions>
      </worksheet>
    </workbook>
  </xsl:template>
</xsl:stylesheet>



and this is for read XSLT file and transform to excel,

public void ExportNewPatientsToExcel()
    {
        logger.Info("New Patients :: export to excel");
        string fileDirectory = string.Empty;

        if (Session[Constants.SESSION_FILE_DIRECTORY] != null)
            fileDirectory = Session[Constants.SESSION_FILE_DIRECTORY].ToString();
        else
        {
            logger.Error("New Patients::File Cache folder is not set.");
            Response.Redirect(Constants.PAGE_ERROR);
        }

        HttpContext context = HttpContext.Current;

        try
        {           
            string xsltFileName = Context.Server.MapPath(Constants.NEW_PATIENTS_XSLT_FILE_NAME);
            PatientCollection patientCollection = PatientBAO.GetNewPatients(ShowAllPatient);

            if (patientCollection.Count > 0 && patientCollection != null)
            {
                string fileName = PatientBAO.GenerateNewPatientsAsExcel(fileDirectory, xsltFileName, patientCollection);
                logger.Info("New Patients Excel version saved name :" + fileName);
                string fileNamePart = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                fileNamePart = fileNamePart.Substring(fileNamePart.IndexOf("_") + 1);//added to remove session id from file name

                context.Items.Add(Constants.ENABLE_CACHE_SZ, Constants.ENABLE_CACHE);
                context.Response.ClearContent();
                context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileNamePart);
                context.Response.ContentType = "application/octet-stream";
                context.Response.TransmitFile(fileName);                
            }
            else
            {
                ShowPopUp(Resources.Patient.RecordNotFoundToExportExcel);
                logger.Error("New patients data not found for export to excel.");
            }
        }
        catch (Exception exc)
        {
            logger.ErrorException("Error occured while export patient details to excel.", exc);
        }
        finally
        {
            //HttpContext.Current.ApplicationInstance.CompleteRequest();
            context.Response.End();
        }
    }


Hi,
I posted code for you, generate excel file for gridview. I dont know is there is a way to give password for protect a excel file, while generating. Eventhough i give a tip for it. Using XSLT you can generated excel file for a gridview, may have option for protect a excel file in XSLT. You can provide format in XSLT file. Try this tip.

Download source

This is XSLT sample file format for a gridview.

<xsl:stylesheet version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts" xmlns:o="urn:schemas-microsoft-com:office:office"
	xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
  <xsl:template match="NewPatients">
    <workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office">
			xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
			xmlns:html="http://www.w3.org/TR/REC-html40">
      <documentproperties xmlns="urn:schemas-microsoft-com:office:office"></documentproperties>
      <excelworkbook xmlns="urn:schemas-microsoft-com:office:excel"></excelworkbook>
      <styles>
        <style ss:id="Header" xmlns:ss="#unknown">
          <alignment ss:horizontal="Center" ss:vertical="Bottom" ss:wraptext="1" />
          <font ss:fontname="Arial" x:family="Swiss" ss:bold="1" xmlns:x="#unknown" />
        </style>
        <style ss:id="HeaderTop" xmlns:ss="#unknown">
          <alignment ss:horizontal="Left" ss:vertical="Top" ss:wraptext="0" />
          <font ss:fontname="Arial" x:family="Swiss" ss:bold="1" xmlns:x="#unknown" />
        </style>
        <style ss:id="HeaderYellow" xmlns:ss="#unknown">
          <alignment ss:horizontal="Center" ss:vertical="Bottom" ss:wraptext="1" />
          <borders>
            <border ss:position="Bottom" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Left" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Right" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Top" ss:linestyle="Continuous" ss:weight="1" />
          </borders>
          <font ss:fontname="MS Sans Serif" x:family="Swiss" ss:color="#FF0000" ss:bold="1" xmlns:x="#unknown" />
          <interior ss:color="#FFFF00" ss:pattern="Solid" />
        </style>
        <style ss:id="Percentage" xmlns:ss="#unknown">
          <numberformat ss:format="0\%" />
        </style>
        <style ss:id="HeaderStyle" xmlns:ss="#unknown">
          <alignment ss:horizontal="Center" ss:vertical="Center" ss:wraptext="1" />
          <borders>
            <border ss:position="Bottom" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Top" ss:linestyle="Continuous" ss:weight="1" />
          </borders>
          <font ss:fontname="Arial" x:family="Swiss" ss:size="9" ss:color="#000000" ss:bold="1" xmlns:x="#unknown" />
          <interior ss:color="#D8D8D8" ss:pattern="Solid" />
          <numberformat ss:format="@" />
        </style>

        <style ss:id="StringDataStyle" xmlns:ss="#unknown">
          <alignment ss:horizontal="Left" ss:vertical="Top" ss:wraptext="1" />
          <borders>
            <border ss:position="Bottom" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Top" ss:linestyle="Continuous" ss:weight="1" />
          </borders>
          <font ss:fontname="Arial" x:family="Swiss" ss:size="9" ss:color="#000000" xmlns:x="#unknown" />
          <numberformat ss:format="@" />
        </style>
        <style ss:id="SpecificStringDataStyle" xmlns:ss="#unknown">
          <alignment ss:horizontal="Right" ss:vertical="Top" ss:wraptext="1" />
          <borders>
            <border ss:position="Bottom" ss:linestyle="Continuous" ss:weight="1" />
            <border ss:position="Top" ss:linestyle="Continuous" ss:weight="1" />
          </borders>
          <font ss:fontname="Arial" x:family="Swiss" ss:size="9" ss:color="#000000" xmlns:x="#unknown" />
          <numberformat ss:format="@" />
        </style>
      </styles>
      <worksheet ss:name="New Patients" xmlns:ss="#unknown">
        <table xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <column ss:autofitwidth="0" ss:width="90" />
          <column ss:autofitwidth="0" ss:width="110" />
          <column ss:autofitwidth="0" ss:width="110" />
          <column ss:autofitwidth="0" ss:width="70" />
          <column ss:autofitwidth="0" ss:width="90" />
          <column ss:autofitwidth="0" ss:width="130" />
          <column ss:autofitwidth="0" ss:width="90" />
          <column ss:autofitwidth="0" ss:width="90" />
          <column ss:autofitwidth="0" ss:width="100" />
          <column ss:autofitwidth="0" ss:width="100" />
          <row>
            <cell ss:styleid="HeaderTop">
              <data ss:type="String">InfuScience - Clinical Progress Tracking</data>
            </cell>
          </row>
          <row>
            <cell ss:styleid="HeaderTop">
              <data ss:type="String">New Patients</data>
            </cell>
          </row>         
          <row>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">MRN</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">First Name</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Last Name</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Therapy</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Sales Rep.</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Physician</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Status</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Start Date</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Tracking Count</data>
            </cell>
            <cell ss:styleid="HeaderStyle">
              <data ss:type="String">Tracked Count</data>
            </cell>
          </row>
          <xsl:for-each select="Patient">
            <row>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="MRN" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="FirstName" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="LastName" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="Therapy" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="SalesRep" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="Physician" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="Status" />
                </data>
              </cell>
              <cell ss:styleid="StringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="StartDate" />
                </data>
              </cell>
              <cell ss:styleid="SpecificStringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="TrackingCount" />
                </data>
              </cell>
              <cell ss:styleid="SpecificStringDataStyle">
                <data ss:type="String">
                  <xsl:value-of select="TrackedCount" />
                </data>
              </cell>              
            </row>
          </xsl:for-each>

        </table>
        <worksheetoptions xmlns="urn:schemas-microsoft-com:office:excel">
          <selected />
          <freezepanes />
          <frozennosplit />
          <splithorizontal>3</splithorizontal>
          <toprowbottompane>3</toprowbottompane>
          <activepane>2</activepane>
          <panes>
            <pane>
              <number>3</number>
            </pane>
            <pane>
              <number>2</number>
              <activerow>2</activerow>
            </pane>
          </panes>
          <protectobjects>False</protectobjects>
          <protectscenarios>False</protectscenarios>
        </worksheetoptions>
      </worksheet>
    </workbook>
  </xsl:template>
</xsl:stylesheet>



and this is for read XSLT file and transform to excel,

public void ExportNewPatientsToExcel()
    {
        logger.Info("New Patients :: export to excel");
        string fileDirectory = string.Empty;

        if (Session[Constants.SESSION_FILE_DIRECTORY] != null)
            fileDirectory = Session[Constants.SESSION_FILE_DIRECTORY].ToString();
        else
        {
            logger.Error("New Patients::File Cache folder is not set.");
            Response.Redirect(Constants.PAGE_ERROR);
        }

        HttpContext context = HttpContext.Current;

        try
        {           
            string xsltFileName = Context.Server.MapPath(Constants.NEW_PATIENTS_XSLT_FILE_NAME);
            PatientCollection patientCollection = PatientBAO.GetNewPatients(ShowAllPatient);

            if (patientCollection.Count > 0 && patientCollection != null)
            {
                string fileName = PatientBAO.GenerateNewPatientsAsExcel(fileDirectory, xsltFileName, patientCollection);
                logger.Info("New Patients Excel version saved name :" + fileName);
                string fileNamePart = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                fileNamePart = fileNamePart.Substring(fileNamePart.IndexOf("_") + 1);//added to remove session id from file name

                context.Items.Add(Constants.ENABLE_CACHE_SZ, Constants.ENABLE_CACHE);
                context.Response.ClearContent();
                context.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileNamePart);
                context.Response.ContentType = "application/octet-stream";
                context.Response.TransmitFile(fileName);                
            }
            else
            {
                ShowPopUp(Resources.Patient.RecordNotFoundToExportExcel);
                logger.Error("New patients data not found for export to excel.");
            }
        }
        catch (Exception exc)
        {
            logger.ErrorException("Error occured while export patient details to excel.", exc);
        }
        finally
        {
            //HttpContext.Current.ApplicationInstance.CompleteRequest();
            context.Response.End();
        }
    }


这篇关于如何在c#中从网格视图创建一个受传递保护的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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