gpt4 book ai didi

c# - 如何在客户端保存导出的Excel文件?

转载 作者:太空宇宙 更新时间:2023-11-03 10:26:38 27 4
gpt4 key购买 nike

我已经使用 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);
}

谢谢,普纳。

最佳答案

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();
}

关于c# - 如何在客户端保存导出的Excel文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399332/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com