//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Collections.Generic;
using System.IO;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage
{
///
/// Writer for writing rows of results to a Excel file
///
public class SaveAsExcelFileStreamWriter : SaveAsStreamWriter
{
#region Member Variables
private readonly SaveResultsAsExcelRequestParams saveParams;
private bool headerWritten;
private SaveAsExcelFileStreamWriterHelper helper;
private SaveAsExcelFileStreamWriterHelper.ExcelSheet sheet;
#endregion
///
/// Constructor, stores the Excel specific request params locally, chains into the base
/// constructor
///
/// FileStream to access the Excel file output
/// Excel save as request parameters
public SaveAsExcelFileStreamWriter(Stream stream, SaveResultsAsExcelRequestParams requestParams)
: base(stream, requestParams)
{
saveParams = requestParams;
helper = new SaveAsExcelFileStreamWriterHelper(stream);
sheet = helper.AddSheet();
}
///
/// Writes a row of data as a Excel row. If this is the first row and the user has requested
/// it, the headers for the column will be emitted as well.
///
/// The data of the row to output to the file
///
/// The entire list of columns for the result set. They will be filtered down as per the
/// request params.
///
public override void WriteRow(IList row, IList columns)
{
int columnStart = ColumnStartIndex ?? 0;
int columnEnd = (ColumnEndIndex != null) ? ColumnEndIndex.Value + 1 : columns.Count;
// Write out the header if we haven't already and the user chose to have it
if (saveParams.IncludeHeaders && !headerWritten)
{
sheet.AddRow();
for (int i = columnStart; i < columnEnd; i++)
{
sheet.AddCell(columns[i].ColumnName);
}
headerWritten = true;
}
sheet.AddRow();
for (int i = columnStart; i < columnEnd; i++)
{
sheet.AddCell(row[i]);
}
}
private bool disposed;
override protected void Dispose(bool disposing)
{
if (disposed)
return;
sheet.Dispose();
helper.Dispose();
disposed = true;
base.Dispose(disposing);
}
}
}