Hello,i am working on downloading the excel file with encryption
I have been successfull till converting and downloading the excel..
but i want the excel with sql data to be saved in a folder and then i want that file to be converted in to encrypted file and then download...
can anyone help me with this..????
my code is like???
protected void btnsubmit_Click(object sender, EventArgs e)
{
try
{
var repository = Context.User as UserPrincipal;
SqlDataReader drr = obj.GetTransactionDetailsByTransactionIdforExcelExport(repository.branchid, txtdate.Text);
if (drr.HasRows)
{
using (DataTable dt = new DataTable())
{
dt.Load(drr);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "Transaction Report By BranchId");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xlsx");
string renderedGridView = wb.ToString();
string date = DateTime.Now.ToString();
File.WriteAllText(@"D:\TFS\MMTS\MMTS.Web\Documents\" + repository.branchid + "" + "dataexcel.xlsx", renderedGridView);
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
string filenames = repository.branchid + "" + "dataexcel.xlsx";
string withoutextntn = repository.branchid + "" + "dataexcel";
string withextntn = (@"D:\TFS\MMTS\MMTS.Web\Documents\" + "" + filenames);
string[] file = Directory.GetFiles(Server.MapPath("~/Documents/"));
string input = Server.MapPath("~/Documents/") + withoutextntn + ".xlsx";
string output = Server.MapPath("~/Documents/") + filenames + "_enc" + ".xlsx";
foreach (string name in file)
{
if (withextntn == name)
{
File.WriteAllText(input, name.ToString());
this.Encrypt(input, output);
Response.Clear();
Response.AddHeader("Content-Dispositions", "attachment; filename=" + Path.GetFileName(output));
Response.WriteFile(output);
Response.Flush();
//Delete the original (input) and the encrypted (output) file.
//File.Delete(input);
//File.Delete(output);
}
}
WriteUserlog.userLogXml(Page.Request.Url.Segments[Page.Request.Url.Segments.Length - 1], repository.userid, "", "Showing Transaction Details By BranchId for Excel Exporting =" + txtdate.Text);
ClearControlFields.ClearAllControls(Page.Controls);
}
else
{
MMTSApplication master = (MMTSApplication)this.Page.Master;
master.ShowErrorMessage(
string.Format(
"{0}<br> Message: {1} "
, CommonValues.MessageBoxHeader
, "No Records Found"
));
}
}
catch (Exception ex)
{
string strError = string.Empty;
ErrorHandler.ShowError(ex, Page.Request.Url.Segments[Page.Request.Url.Segments.Length - 1],
System.Reflection.MethodBase.GetCurrentMethod().Name, out strError, HttpContext.Current.User.Identity.Name);
MMTSApplication master = (MMTSApplication)this.Page.Master;
master.ShowErrorMessage(
string.Format(
"{0}<br> Message: {1} "
, CommonValues.MessageBoxHeader
, strError
));
}
}
private void Encrypt(string inputFilePath, string outputfilePath)
{
string EncryptionKey = "MAKV2SPBNI99212";
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
{
using (CryptoStream cs = new CryptoStream(fsOutput, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
{
int data;
while ((data = fsInput.ReadByte()) != -1)
{
cs.WriteByte((byte)data);
}
}
}
}
}
}
i m not getting the converted excel ...can u tell me the error...