该进程无法访问该文件,因为它正由另一个进程使用(下载动态生成的文件)

时间:2016-12-09 02:22:00

标签: c# io

点击按钮后,用户应该可以下载QueryReuslt.xlsx,这个文件是用数据库查询结果生成的。是错误说2个用户同时尝试读取QueryResult.xlsx?我该怎样避免这个?

try
    {
        getDataForExcel();
        String fileName = "QueryResult.xlsx";

        // Construct the datatable programetically
        DataTable datatable = ds.Tables[0];

        string XLSPath = ConfigurationManager.AppSettings["EXPORTPATH"].ToString();//temp path to store the XLS file

        string excelFileName = XLSPath + @"\QueryResult.xlsx";

        ExportDataSet(datatable, excelFileName);

        // write file to client
        FileInfo file = new FileInfo(excelFileName);

        if (file.Exists)
        {
            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            Response.AddHeader("Content-Type", "application/Excel");
            Response.ContentType = "application/vnd.xls";
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.End();
        }
        else
        {
            Response.Write("This file does not exist.");
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }


private void ExportDataSet(DataTable table, string excelFileName)
{
    using (var workbook = SpreadsheetDocument.Create(excelFileName, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
    {
        var workbookPart = workbook.AddWorkbookPart();

        workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

        workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();

        var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
        var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
        sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);

        DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
        string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);

        uint sheetId = 1;
        if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
        {
            sheetId =
                sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
        }

        DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
        sheets.Append(sheet);

        DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();


        // Construct column names
        List<String> columns = new List<string>();
        foreach (System.Data.DataColumn column in table.Columns)
        {
            columns.Add(column.ColumnName);

            DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
            cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
            cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
            headerRow.AppendChild(cell);
        }

        // Add the row values to the excel sheet
        sheetData.AppendChild(headerRow);

        foreach (System.Data.DataRow dsrow in table.Rows)
        {
            DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
            foreach (String col in columns)
            {
                DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString());
                newRow.AppendChild(cell);
            }

            sheetData.AppendChild(newRow);
        }
    }
}

0 个答案:

没有答案