如何在客户端保存导出的Excel文件
本文关键字:Excel 文件 保存 客户端 | 更新日期: 2023-09-27 18:02:36
我已经使用Microsoft.Office.Interop.Excel dll将数据导出到excel,它工作正常,但它在服务器位置下载,而不是在客户端位置。我们如何在客户端位置下载文件。这是我用来导出到excel的代码,以及我们如何要求保存excel文件的位置。
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);
for (int i = 1; i < dsColors.Tables[0].Rows.Count; i++)
{
ExcelWorkBook.Worksheets.Add(); //Adding New sheet in Excel Workbook
}
for (int i = 0; i < 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
for (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 into Excel Sheet
for (int row = 0; row < table.Rows.Count; row++) //r stands for ExcelRow and col for ExcelColumn
{
// Excel row and column start positions for writing Row=1 and Col=1
for (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() + "'"));
for (int col = 1; col <= table.Columns.Count; col++)
{
ExcelWorkSheet.Cells[r, col] = "";
}
r++;
for (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 the 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);
}
谢谢,Purna .
using (MemoryStream memoryStream = new MemoryStream())
{
using (var writer = new StreamWriter(memoryStream))
{
// your code to write the 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();
}
只需在代码底部添加以下代码:
var excelFilePathMap = Server.MapPath(FileName);
return new FilePathResult(excelFilePathMap, "application/octet-stream");
。在代码的最后一行之后:Marshal.ReleaseComObject(ExcelApp);
它会要求用户下载文件