0

I am using ClosedXML from this example. Exporting datasets into multiple excel sheets of one excel file

My code is mostly the same. I get ReadTimeOut and WriteTimeOut error and thus, no file is created. (Please see attachment).

here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ClosedXML.Excel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mvc;

namespace MyProject.DAL
{
    public class GenerateBook
    {
        private DataTable get_TblA()
        {

            using (SqlConnection con = Connection.GetConnection())
            {
         using (SqlCommand cmd = new SqlCommand("SELECT cola, colB
                                FROM dbo.tblA;"))
                {


      using (SqlDataAdapter da = new SqlDataAdapter())
                {
                    DataTable dt = new DataTable();
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    return dt;
                }
            }
        }
    }

    private DataTable get_TblB()
    {

        using (SqlConnection con = Connection.GetConnection())
        {
            using (SqlCommand cmd = new SqlCommand("SELECT cola, colB
    FROM dbo.tblB;"))
            {
                using (SqlDataAdapter da = new SqlDataAdapter())
                {
                    DataTable dt = new DataTable();
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    return dt;
                }
            }
        }
    }

    public DataSet getDataSetExportToExcel()
    {
        DataSet ds = new DataSet();
        DataTable dtEmp = new DataTable("ALM_List_LOB");
        dtEmp = getUIP_ALM_List();

        DataTable dtEmpOrder = new DataTable("GPR_LOB_CMS_VDN");
        dtEmpOrder = getGPR_LOB_CMS_VDN();
        ds.Tables.Add(dtEmp);
        ds.Tables.Add(dtEmpOrder);
        return ds;
    }
    public DataSet GetDataSetExportToExcel()
    {
        //use for multiple sps
        DataSet ds = new DataSet();
        var SPNames = new List<string>() { "storedproc1", "storedproc2" };

        foreach (var SPName in SPNames)
        {
            DataTable dt = new DataTable();
            dt = GetDataTableExportToExcel(SPName);
            ds.Tables.Add(dt);
        }

        return ds;
    }
    private DataTable GetDataTableExportToExcel(string SPName)
    {
        //use for single sp
        DataTable dt = new DataTable();

        using (SqlConnection con = Connection.GetConnection())
        {
            using (var cmd = new SqlCommand(SPName, con))
            {
                using (var sda = new SqlDataAdapter(cmd))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    //cmd.Parameters.AddWithValue()
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
    public ActionResult ExportToExcel()
    {
        try
        {
            var sheetNames = new List<string>() { "sheetName1", "sheetName2" };
            string fileName = "Example.xlsx";

            //  for sps DataSet ds = GetDataSetExportToExcel();
            DataSet ds = getDataSetExportToExcel();
            XLWorkbook wbook = new XLWorkbook();

            for (int k = 0; k < ds.Tables.Count; k++)
            {
                DataTable dt = ds.Tables[k];
                IXLWorksheet Sheet = wbook.Worksheets.Add(sheetNames[k]);
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    Sheet.Cell(1, (i + 1)).Value = dt.Columns[i].ColumnName;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Sheet.Cell((i + 2), (j + 1)).Value = dt.Rows[i][j].ToString();
                    }
                }
            }


            Stream spreadsheetStream = new MemoryStream();
            wbook.SaveAs(spreadsheetStream);
            spreadsheetStream.Position = 0;

            return new FileStreamResult(spreadsheetStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = fileName };
        }
        catch(Exception e)
        {
            throw e;
        }
    }
    public SetToExport(string channel, string assets )
    {
        ActionResult status = ExportToExcel();
    }
}

}

I tried setting them to 0 but did not help. Please guide how to fix I have tried various samples to achieve this, but none have worked completely Really will appreciate help.

SilverFish
  • 1,014
  • 6
  • 28
  • 65
  • try changing to `MemoryStream spreadsheetStream = new MemoryStream();`. As i met similar problem earlier, i found solution this way. – Farshan Jan 16 '19 at 21:33
  • I still get the same error as in image. The code does not throw any exception and neither does it produce file. Is it supposed to open the Excel file or save somewhere? I have not provided any file path – SilverFish Jan 16 '19 at 21:59
  • Is data present in the rows you retrieved from database? – Farshan Jan 16 '19 at 22:00
  • Yes, I see both datasets. Even ActionResult gives same error for FileStream ReadTimeout and WriteTimeout. – SilverFish Jan 16 '19 at 22:03
  • As i can see from your code, you are returning the excel stream from your function. As it is memorystream you don't need to specify any location. – Farshan Jan 16 '19 at 22:05
  • i'm returning this file `PhysicalFile(data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");` from my asp.net core project. I don't know of this helps. – Farshan Jan 16 '19 at 22:09
  • What is PhysicalFile? Do you have any other references? Can you please share your code? I do not have core project though. Just MVC (with C# and Angular) but this is in a class file in data access layer, so I don't think should matter – SilverFish Jan 16 '19 at 22:19
  • You are right - it is ControllerBase.PhysicalFile Method from Microsoft.AspNetCore.Mvc – SilverFish Jan 16 '19 at 22:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186815/discussion-between-farshan-and-silverfish). – Farshan Jan 16 '19 at 22:22

0 Answers0