Ejemplo n.º 1
0
        public bool ExportGenreXLSX(ZOperationResult operationResult, string fileDirectory,
                                    out string filePath)
        {
            string template = "Genre";
            string file     = template + "." + String.Format("{0:yyyyMMdd.HHmmssfff}", DateTime.Now) + ".xlsx";

            filePath = Path.Combine(fileDirectory, file);

            DbConnection connection = null;

            ExcelEngine excelEngine = null;

            try
            {
                DbProviderFactory provider;
                string            connectionName = "Chinook";

                provider   = AdoNetHelper.GetProvider(connectionName);
                connection = provider.CreateConnection();
                connection.ConnectionString = AdoNetHelper.GetConnectionString(connectionName);
                connection.Open();

                DbCommand    command;
                DbDataReader reader;
                //DbParameter parameter;

                command                = provider.CreateCommand();
                command.Connection     = connection;
                command.CommandTimeout = 600;
                command.CommandType    = System.Data.CommandType.Text;
                command.CommandText    = @"
SELECT
    GenreId
    ,Name
FROM
    Genre
ORDER BY
    Genre.Name
";

                //parameter = command.CreateParameter();
                //parameter.DbType = DbType.DateTime;
                //parameter.ParameterName = "@Data";
                //parameter.Value = viewModel.XDateTime;
                //command.Parameters.Add(parameter);

                excelEngine = new ExcelEngine();
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
                IWorkbook workbook = application.Workbooks.Create(1);
                //workbook.Version = ExcelVersion.Excel2013;
                IWorksheet worksheet = workbook.Worksheets[0];

                worksheet.Range[1, 1].Text = "GenreId";
                worksheet.Range[1, 2].Text = "Name";

                reader = command.ExecuteReader();

                int row = 2;

                while (reader.Read())
                {
                    int column = 1;

                    worksheet.Range[row, column++].Value2 = reader.ToInt32("GenreId");
                    worksheet.Range[row, column++].Value2 = reader.ToString("Name");

                    row++;
                }

                reader.Close();

                worksheet.AutoAlign(1, 2);
                workbook.SaveAs(filePath);
                workbook.Close();
            }
            catch (Exception exception)
            {
                operationResult.ParseException(exception);
            }
            finally
            {
                if (excelEngine != null)
                {
                    excelEngine.Dispose();
                }

                if (connection != null)
                {
                    connection.Close();
                }
            }

            return(operationResult.Ok);
        }