c# - How to save the exported Excel file in client place? -
i have exported data excel using microsoft.office.interop.excel dll working fine downloading in server location not in client location. how can download file in client location. here code have used exporting excel , how can ask location save excel file .
dataset dscolors = objclscorp.getblockcolorsforreport(); if (dsreport.tables.count > 0 && dscolors.tables.count > 0) { application excelapp = new application(); workbook excelworkbook = null; worksheet excelworksheet = null; excelapp.visible = true; excelworkbook = excelapp.workbooks.add(xlwbatemplate.xlwbatworksheet); (int = 1; < dscolors.tables[0].rows.count; i++) { excelworkbook.worksheets.add(); //adding new sheet in excel workbook } (int = 0; < dscolors.tables[0].rows.count; i++) { int r = 1; // initialize excel row start position = 1 excelworksheet = (microsoft.office.interop.excel.worksheet)excelworkbook.worksheets[i + 1]; //writing columns name in excel sheet (int col = 1; col <= dsreport.tables[0].columns.count; col++) { excelworksheet.cells[r, col] = dsreport.tables[0].columns[col - 1].columnname; } r++; system.data.datatable table = dsreport.tables[0]; if (table.rows.count > 0) { table.defaultview.rowfilter = "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring()+"'"; table = table.defaultview.totable(); //writing rows excel sheet (int row = 0; row < table.rows.count; row++) //r stands excelrow , col excelcolumn { // excel row , column start positions writing row=1 , col=1 (int col = 1; col <= table.columns.count; col++) { excelworksheet.cells[r, col] = table.rows[row][col - 1].tostring(); } r++; } } if (table.rows.count > 0) { int totalstock = 0; decimal totalbqtysft = 0; int totaltodaymarked = 0; decimal totalmqtysft = 0; int totalmarked = 0; decimal totaltmqtysft = 0; int totalbalancenos = 0; decimal totalgrandqtysft = 0; totalstock = convert.toint32(table.compute("sum(stock)", "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring() + "'")); totalbqtysft = convert.todecimal(table.compute("sum(bqtysft)", "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring() + "'")); totaltodaymarked = convert.toint32(table.compute("sum(todaymarked)", "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring() + "'")); totalmqtysft = convert.todecimal(table.compute("sum(mqtysft)", "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring() + "'")); totalmarked = convert.toint32(table.compute("sum(totalmarked)", "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring() + "'")); totaltmqtysft = convert.todecimal(table.compute("sum(tmqtysft)", "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring() + "'")); totalbalancenos = convert.toint32(table.compute("sum(balancenos)", "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring() + "'")); totalgrandqtysft = convert.todecimal(table.compute("sum(grandqtysft)", "color = '" + dscolors.tables[0].rows[i]["blockcolor"].tostring() + "'")); (int col = 1; col <= table.columns.count; col++) { excelworksheet.cells[r, col] = ""; } r++; (int col = 1; col <= table.columns.count; col++) { if (table.columns[col - 1].columnname == "stock") { excelworksheet.cells[r, col] = totalstock.tostring(); } if (table.columns[col - 1].columnname == "bqtysft") { excelworksheet.cells[r, col] = totalbqtysft.tostring(); } if (table.columns[col - 1].columnname == "todaymarked") { excelworksheet.cells[r, col] = totaltodaymarked.tostring(); } if (table.columns[col - 1].columnname == "mqtysft") { excelworksheet.cells[r, col] = totalmqtysft.tostring(); } if (table.columns[col - 1].columnname == "totalmarked") { excelworksheet.cells[r, col] = totalmarked.tostring(); } if (table.columns[col - 1].columnname == "tmqtysft") { excelworksheet.cells[r, col] = totaltmqtysft.tostring(); } if (table.columns[col - 1].columnname == "balancenos") { excelworksheet.cells[r, col] = totalbalancenos.tostring(); } if (table.columns[col - 1].columnname == "grandqtysft") { excelworksheet.cells[r, col] = totalgrandqtysft.tostring(); } } r++; } try { excelworksheet.name = dscolors.tables[0].rows[i]["abbreviation"].tostring();//renaming excelsheets } catch (exception ex) { excelworksheet.name = dscolors.tables[0].rows[i]["abbreviation"].tostring() + string.format("{0:d}", math.abs(i - datetime.now.millisecond)); } } string filepath = "c:\\netstockreport\\"; if (!directory.exists(filepath)) { directory.createdirectory(filepath); } string filename = filepath+"netstockreport" + datetime.now.tostring("dd-mm-yyyy-hh-mm-ss")+".xlsx"; excelworkbook.saveas(filename); excelworkbook.close(); excelapp.quit(); marshal.releasecomobject(excelworksheet); marshal.releasecomobject(excelworkbook); marshal.releasecomobject(excelapp); }
thanks, purna.
using (memorystream memorystream = new memorystream()) { using (var writer = new streamwriter(memorystream)) { // code write file content } string filename = "yourfilename.csv"; httpcontext.current.response.clear(); httpcontext.current.response.contenttype = "text/csv"; httpcontext.current.response.addheader("content-disposition", "attachment;filename=" + filename); httpcontext.current.response.cache.setcacheability(httpcacheability.nocache); httpcontext.current.response.binarywrite(memorystream.toarray()); httpcontext.current.response.end(); }
Comments
Post a Comment