Change Excel Headings Based on Radio Button selection of Grams or Kilograms.
I have a radio button with the items Grams and Kilograms and a button to export to excel.
When I select radio button Grams i want to change my headings to Grams in my export and when I select Kilograms I want to change my heading to Kilograms in my excel sheet.
Below is my Radio Button:
<asp:RadioButtonList ID="optMetric" CssClass="Space" runat="server">
<asp:ListItem Selected="True" Value="0" Text="Kilograms"></asp:ListItem>
<asp:ListItem Value="1" Text="Grams"></asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="btnExportToExcel" runat="server" ValidationGroup="1" CausesValidation="true" OnClick="btnExportToExcel_Click" CssClass="btn btn-info btn-md" Text="Export To Excel" />
Below is my Export To Excel Code:
mCWC = New aplComponentWeightChecks(My.Settings.Quality)
Dim dt As DataTable = mCWC.GetLast10AverageComponents(strStockCode, dtFromDate, dtToDate, blnOutOfSpec, blnShowInKg)
Dim intRow As Integer = 2
Dim strStatus As String = ""
If dt.Rows.Count > 0 Then
Dim strFileName As String = "\\Webstor\ExcelReports$\Component Weight Check Report.xlsx"
If IO.File.Exists(strFileName) Then IO.File.Delete(strFileName)
LTGlobals.OpenExcelFile("\\Webstor\ExcelReports$\ComponentWeightCheckReport.xlsx")
LTGlobals.DisableOpenExcelSettings()
For i As Integer = 0 To dt.Rows.Count - 1
LTGlobals.xlSheetFile.cells(intRow, 1) = Format(dt.Rows(i)("DateCaptured"), "MM/dd/yyyy")
LTGlobals.xlSheetFile.cells(intRow, 2) = dt.Rows(i)("StockCode")
LTGlobals.xlSheetFile.cells(intRow, 3) = dt.Rows(i)("LongDesc")
LTGlobals.xlSheetFile.cells(intRow, 4) = dt.Rows(i)("PartWeightAverage")
LTGlobals.xlSheetFile.cells(intRow, 5) = dt.Rows(i)("SprueWeightAverage")
LTGlobals.xlSheetFile.cells(intRow, 6) = dt.Rows(i)("TolerancePercentageAverage")
LTGlobals.xlSheetFile.cells(intRow, 7) = dt.Rows(i)("BomWeightAverage")
LTGlobals.xlSheetFile.cells(intRow, 8) = dt.Rows(i)("VarianceToSysproAverage")
LTGlobals.xlSheetFile.cells(intRow, 9) = dt.Rows(i)("VarianceToSysproPctAverage")
LTGlobals.xlSheetFile.cells(intRow, 10) = dt.Rows(i)("IsOutOfSpecAverage")
intRow = intRow + 1
Next
LTGlobals.xlBookFile.SaveAs(strFileName)
LTGlobals.xlBookFile.Close(SaveChanges:=False)
LTGlobals.EnableOpenExcelSettings()
LTGlobals.xlAppFile.Quit()
LTGlobals.xlAppFile = Nothing
LTGlobals.xlBookFile = Nothing
LTGlobals.xlSheetFile = Nothing
LTGlobals.Kill_Excel()
Dim fileDet As New System.IO.FileInfo(strFileName)
Response.Clear()
Response.Charset = "UTF-8"
Response.ContentEncoding = Encoding.UTF8
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileDet.Name)
Response.AddHeader("Content-Length", fileDet.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.TransmitFile(fileDet.FullName)
Response.End()
End If
End If