Click here to Skip to main content
15,904,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

 
Share this answer
 
v2
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.
HTML
<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,

C#
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();
        }
    }
 
Share this answer
 
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.
HTML
<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,

C#
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();
        }
    }
 
Share this answer
 
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.

Source

This is XSLT sample file format for a gridview.
XML
<?xml version="1.0" encoding="utf-8"?>
<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">
          <Alignment ss:Horizontal="Center" ss:Vertical="Bottom" ss:WrapText="1" />
          <Font ss:FontName="Arial" x:Family="Swiss" ss:Bold="1" />
        </Style>
        <Style ss:ID="HeaderTop">
          <Alignment ss:Horizontal='Left' ss:Vertical='Top' ss:WrapText='0' />
          <Font ss:FontName='Arial' x:Family='Swiss' ss:Bold='1' />
        </Style>
        <Style ss:ID="HeaderYellow">
          <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" />
          <Interior ss:Color="#FFFF00" ss:Pattern="Solid" />
        </Style>
        <Style ss:ID="Percentage">
          <NumberFormat ss:Format="0\%" />
        </Style>
        <Style ss:ID="HeaderStyle">
          <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" />
          <Interior ss:Color="#D8D8D8" ss:Pattern="Solid" />
          <NumberFormat ss:Format="@" />
        </Style>

        <Style ss:ID="StringDataStyle">
          <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" />
          <NumberFormat ss:Format="@" />
        </Style>
        <Style ss:ID="SpecificStringDataStyle">
          <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" />
          <NumberFormat ss:Format="@" />
        </Style>
      </Styles>
      <Worksheet ss:Name="New Patients">
        <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,

C#
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();
        }
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900