opening an existing empty spreadsheet and writing data into it - asp.net

I have a spreadsheet with multiple pages in it.When I click on a button I need to open this spreadsheet and write all the data(dataset/datatable) returned from the database into one of the pages in the spreadsheet.I saw so many articles for exporting dataset to a new excel sheet.how do i open an existing spreadsheet and write a dataset into it using asp.net/C#?
Please help..
Thanks.
UPDATE:
Basically I have the following code to export a dataset to a new excel sheet.
private void createDataInExcel(DataSet ds)
{
Application oXL;
_Workbook oWB;
_Worksheet oSheet;
Range oRng;
string strCurrentDir = Server.MapPath(".") + "\\excelreports\\";
try
{
oXL = new Application();
oXL.Visible = false;
//Get a new workbook.
oWB = (_Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (_Worksheet)oWB.ActiveSheet;
//System.Data.DataTable dtGridData=ds.Tables[0];
int iRow = 2;
if (ds.Tables[0].Rows.Count > 0)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
oSheet.Cells[1, j + 1] = ds.Tables[0].Columns[j].ColumnName;
}
// For each row, print the values of each column.
for (int rowNo = 0; rowNo < ds.Tables[0].Rows.Count; rowNo++)
{
for (int colNo = 0; colNo < ds.Tables[0].Columns.Count; colNo++)
{
oSheet.Cells[iRow, colNo + 1] = ds.Tables[0].Rows[rowNo][colNo].ToString();
}
iRow++;
}
}
oRng = oSheet.get_Range("A1", "IV1");
oRng.EntireColumn.AutoFit();
oXL.Visible = false;
oXL.UserControl = false;
string strFile = "excelreport" + DateTime.Now.Ticks.ToString() + ".xls";//+
oWB.SaveAs(strCurrentDir +strFile, XlFileFormat.xlWorkbookNormal, null, null, false, false, XlSaveAsAccessMode.xlShared, false, false, null,null, null);
// Need all following code to clean up and remove all references!!!
oWB.Close(null, null, null);
oXL.Workbooks.Close();
oXL.Quit();
Marshal.ReleaseComObject(oRng);
Marshal.ReleaseComObject(oXL);
Marshal.ReleaseComObject(oSheet);
Marshal.ReleaseComObject(oWB);
}
catch (Exception theException)
{
Response.Write(theException.Message);
}
Response.Write("data exported");
}
Is it possible to improve the above code to write the dataset to an existing sheet?Also with the above code its taking about a minute to write the data into excel sheet..I do not understand why is it taking that long.

not 100% sure where you are with your code, however using the excel com object referenced in your project you can open a workbook using the Workbooks._Open method, then you can get the sheet by name using the sheets collection of the workbook and the get_Item.
if you need to add a sheet to the workbook you can use the add on the sheets collect.
Maybe if you post the code you have we can suggest where to improve it.
this line
oWB = (_Workbook)(oXL.Workbooks.Add(Missing.Value));
is creating a new workbook. use
string workbookPath = "c:/SomeWorkBook.xls";
oWB = Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
now it depends on what you want to do add a new sheet, use an existing sheet etc.
this is a codeproject link that shows more in depth here

Related

Error message "entries cannot be opened multiple times in update mode." in Spreadsheet Lite SaveAs function

Upon execution of the dBWorksheet.SaveAs(xlsFileSpec), in the code below, I am seeing an exception:
"entries cannot be opened multiple times in update mode."
SLDocument dBWorksheet = new SLDocument();
TimeSpan interval = new TimeSpan(0, 0, 2);
dBWorksheet.SetCellValue(2, 1, "Hour");
dBWorksheet.SetCellValue(3, 1, "Time");
int Row = 3;
// Create the hour and time of day columns.
for(TimeSpan dBTime = new TimeSpan(0, 0, 0); dBTime.TotalHours < 24; dBTime = dBTime.Add(interval))
{
dBWorksheet.SetCellValue(Row, 1, dBTime.Hours);
dBWorksheet.SetCellValue(Row, 2, dBTime.ToString());
Row++;
}
// Save the new worksheet.
dBWorksheet.SaveAs(xlsFileSpec);
I had this error when I opened (by SpreadsheetLight) .xlsx file that was saved (by LibreOffice Calc) in "Excel 2007-365" format and then I tried use a SaveAs function.
When I save (by LibreOffice Calc) the .xlsx file as "Office Open XML" then I can Open and SaveAs (by SpreadsheetLight) a .xlsx file without problems.
Here's how I solved it.
Downloaded the source code for SpreadsheetLight (version 3.5).
http://spreadsheetlight.com/downloads/SpreadsheetLight3.5.zip
Created a .NET Core library project with the name "SpreadsheetLight" and added necessary NuGet packages (DocumentFormat.OpenXML and System.Drawing.Common) to it. Copied and pasted all the downloaded source code files in this project.
Added the project "SpreadsheetLight" to my solution and referenced it in one of the existing projects.
In "SLDocument.cs" file, make the following changes in the method "LoadDocumentProperties()" so that the code looks like the following:
// XDocument xdoc = XDocument.Load(XmlReader.Create(xl.CoreFilePropertiesPart.GetStream()));
Stream stream = xl.CoreFilePropertiesPart.GetStream();
XDocument xdoc = XDocument.Load(XmlReader.Create(stream));
foreach (XElement xelem in xdoc.Descendants())
{
// Code omitted.
}
stream.Close();
Build your solution and test it.
Roll-back from .NET Core 3.0 to .NET Framework 4.7.x
This is obviously not the most desirable solution.
However, the only solution that I have found is to roll-back the application from .NET Core 3.0 and SpreadsheetLight.Core to .NET Framework 4.7.x and SpreadsheetLight.
The code posted in the question above runs without modification.
I believe this has to do with a memory leak that was fixed in System.IO.Packaging in .NET Core 3.0. This will require further investigation and probably a fix to SpreadsheetLight.Core.
A bit late to the party but just bumped in to this problem.
I solved this by create a new SLDocument and copied cell by cell from the old SLDocument. Might not work 100%, but it has covered my reports so far.
Code
using (var file = new SLDocument())
{
file.CopyFromTemplate(Path.Combine("ReportTemplates\\Tackningsbidrag_budget.xlsx"), maxCols: 20, maxRows: 10);
// code
using (var ms = new MemoryStream())
{
file.SaveAs(ms);
}
}
Extension method:
public static void CopyFromTemplate(this SLDocument file, string pathToOrgFile, int? maxCols = null, int? maxRows = null)
{
using (var orgFile = new SLDocument(pathToOrgFile))
{
var page = orgFile.GetPageSettings();
file.SetPageSettings(page);
foreach (var cell in orgFile.GetWorksheetMergeCells())
{
file.MergeWorksheetCells(cell.StartRowIndex, cell.StartColumnIndex, cell.EndRowIndex, cell.EndColumnIndex);
}
var stats = orgFile.GetWorksheetStatistics();
var endCol = stats.EndColumnIndex;
if (maxCols.HasValue && maxCols < endCol)
{
endCol = maxCols.Value;
}
var endRow = stats.EndRowIndex;
if (maxRows.HasValue && maxRows < endRow)
{
endRow = maxRows.Value;
}
for (int col = stats.StartColumnIndex; col <= endCol; col++)
{
file.SetColumnStyle(col, orgFile.GetColumnStyle(col));
file.SetColumnWidth(col, orgFile.GetColumnWidth(col));
}
for (int row = stats.StartRowIndex; row <= endRow; row++)
{
file.SetRowStyle(row, orgFile.GetRowStyle(row));
file.SetRowHeight(row, orgFile.GetRowHeight(row));
}
for (int row = stats.StartRowIndex; row <= endRow; row++)
{
for (int col = stats.StartColumnIndex; col <= endCol; col++)
{
var formula = orgFile.GetCellFormula(row, col);
var stringValue = orgFile.GetCellValueAsString(row, col);
file.SetCellValue(row, col, !string.IsNullOrWhiteSpace(formula) ? ("=" + formula) : stringValue);
file.SetCellStyle(row, col, orgFile.GetCellStyle(row, col));
}
}
}
}

Multiple pages in the same PDF reporting with PDFsharp

I am getting values from database and collection of all values are larger than a single page. Number of pages are unknown and it might differ report by report. Currently, only 1 page of multiple pages is being printed but same information in every pages. I want continuous information in the pages not same information in every page. How can I do that with PDFsharp?
Here I am giving my codes to understand...
Document doc = new Document();
//Create table
var sec = doc.AddSection();
var table = sec.AddTable();
table.Format.Font.Size = 6;
table.Borders.Distance = 0;
table.Borders.Color = MigraDoc.DocumentObjectModel.Colors.DarkGray;
var renderer = new DocumentRenderer(doc);
renderer.PrepareDocument();
int totalPage = renderer.FormattedDocument.PageCount;
for (int i = 1; i <= totalPage; ++i)
{
PdfPage pdfPage = pdf.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
renderer.RenderObject(gfx, XUnit.FromMillimeter(10), XUnit.FromMillimeter(10), XUnit.FromMillimeter(double.MaxValue), table);
gfx.Dispose();
}
You can have MigraDoc create the whole document without a for loop:
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = document;
renderer.RenderDocument();
Your for loop has a variable i that is not used in your code. If you want to do it the complicated way, use RenderPage instead of RenderObject.
See also:
http://www.pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx
I'm not sure if this may be of help to anyone but if you want to break your table into multiple pages you can use this method:
public Document MultiplePagesPerDocument(Document document, DocumentForm documentForm, Section section, DocumentPage page, DataTable dt, int maxTableRows)
{
_document = document;
if (dt.Rows.Count > maxTableRows)
{
for (int i = 1; i <= dt.Rows.Count; i++)
{
// Break the table onto next page for maxTableRows
if (i % maxTableRows == 0)
{
section = _document.LastSection;
section.AddPageBreak();
Paragraph paragraph = section.AddParagraph();
paragraph.Format.SpaceAfter = "3cm";
_position = new PageItemPosition()
{
Height = "3.0cm",
Width = "8.0cm",
Top = "3.0cm",
LeftPosition = ShapePosition.Left,
TopPosition = ShapePosition.Top
};
object tableSection = page.AddPageTextFrame(section, _position, string.Empty);
documentForm.GenerateSection(section, tableSection, 170, "Table", dt);
}
}
}
return this._document;
}

asp.net open xml to download .xlsx file

I'm trying to supply a .xlsx file from a grid, I think most of the hard work is done. I'm picking up a template file and filling it with data.
I' getting an error 'file not found' on Response.WriteFile.
by the looks of the example (linked below) this should just be the intended file name, but I imagine this needs to be a path to the file?, so do I need to save my 'myDoc' object to the server and then provide the path in the Reponse.WriteFile.
It doesn't seem like that is what is meant by the example.
the code i'm using is a modified version of : http://technet.weblineindia.com/web/export-data-to-excel-using-openxml-sdk/
due to using sharepoint to store the template file I just had to create a filestream rather than supply the URL to the file.
here is my code:
// Create cell reference array
string[] CellReferenceArray = new string[] { "A", "B", "C", "D", "E" };
//Open your saved excel file that you have created using template file.
using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(file.OpenBinaryStream(), true))
{
// Create reference of main Workbook part, which contains all reference.
WorkbookPart objworkbook = myDoc.WorkbookPart;
// Create style sheet object that will be used for applying styling.
Stylesheet objstyleSheet = objworkbook.WorkbookStylesPart.Stylesheet;
// pick up first worksheet
WorksheetPart objworksheet = objworkbook.WorksheetParts.First();
// will be used in end while creating sheet data
string objorigninalSheetId = objworkbook.GetIdOfPart(objworksheet);
WorksheetPart objreplacementPart = objworkbook.AddNewPart<WorksheetPart>();
string objreplacementPartId = objworkbook.GetIdOfPart(objreplacementPart);
// Create object reader to read from excel file.
OpenXmlReader objreader = OpenXmlReader.Create(objworksheet);
// create writer object to write in excel sheet.
OpenXmlWriter objOpenXmwriter = OpenXmlWriter.Create(objreplacementPart);
int i = 1;
Row r = new Row();
Cell c = new Cell();
Columns col1 = new Columns();
UInt32 index = 0;
while (objreader.Read())
{
if (objreader.ElementType == typeof(SheetData))
{
if (objreader.IsEndElement)
continue;
objOpenXmwriter.WriteStartElement(new SheetData());
objOpenXmwriter.WriteStartElement(r);
// Loop to insert header
foreach (DataColumn colHead in YoutdTName.Columns)
{
c = new Cell
{
DataType = CellValues.String,
CellReference = CellReferenceArray[i] + Convert.ToString(index)
};
CellValue v1 = new CellValue(colHead.ColumnName.ToString());
c.Append(v1);
objOpenXmwriter.WriteElement(c);
i += 1;
}
objOpenXmwriter.WriteEndElement();
index += 1;
//Loop to insert datatable row in excel
foreach (DataRow dr in YoutdTName.Rows)
{
objOpenXmwriter.WriteStartElement(r);
i = 1;
foreach (DataColumn col in YoutdTName.Columns)
{
c = new Cell
{
DataType = CellValues.String,
CellReference = CellReferenceArray[i] + Convert.ToString(index)
};
CellValue v1 = new CellValue(dr[col].ToString());
c.AppendChild(v1);
objOpenXmwriter.WriteElement(c);
i += 1;
}
objOpenXmwriter.WriteEndElement();
index += 1;
}
objOpenXmwriter.WriteEndElement();
}
}
//close all objects
objreader.Close();
objOpenXmwriter.Close();
Sheet sheet = objworkbook.Workbook.Descendants<Sheet>().Where(s => s.Id.Value.Equals(objorigninalSheetId)).First();
sheet.Id.Value = objreplacementPartId;
objworkbook.DeletePart(objworksheet);
}
Response.AddHeader("Content-Disposition", "inline;filename=YourExcelfileName.xlxs");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.WriteFile("YourExcelfileName.xlxs");
Response.Flush();
Response.End();
}
Use HttpResponse.BinaryWrite instead and take the underlying stream from your SpreadsheetDocument-instance .
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.binarywrite(v=vs.110).aspx

How convert stream excel file to datatable C#?

I use Epplus to reading xlsx files from stream.
It has a bug , it cant read some columns in my workbook.How can read xlsx files from stream to datatable without epplus ?
my older code:
public static DataSet ReadExcelFile(Stream stream)
{
try
{
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader =
ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
DataSet result = excelReader.AsDataSet();
return result;
}
catch (Exception x)
{
throw x;
}
}
I didnt report it, but i tried so much combinations.If there are empty columns in worksheet ,epplus reader cant read correctly column values.
"It has a bug , it cant read some columns in my workbook"
Can you describe the bug, have you reported it or is it already known, what version are you using?
Here's a simple approach to load an excel file into a DataTable with EPPlus.
public static DataTable getDataTableFromExcel(string path)
{
using (var pck = new OfficeOpenXml.ExcelPackage())
{
using (var stream = File.OpenRead(path))
{
pck.Load(stream);
}
var ws = pck.Workbook.Worksheets.First();
DataTable tbl = new DataTable();
bool hasHeader = true; // adjust it accordingly( i've mentioned that this is a simple approach)
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
}
var startRow = hasHeader ? 2 : 1;
for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
var row = tbl.NewRow();
foreach (var cell in wsRow)
{
row[cell.Start.Column - 1] = cell.Text;
}
tbl.Rows.Add(row);
}
return tbl;
}
}
This is way past, however it could still help someone.
Apparently some columns in my worksheet were merged, so for example, if columns A and B are merged it only recognizes column A as the one with the value, and so it returns column B as empty, when i call on that particular cell's value(B). To get past this, make sure you know which cells are merged and then grab only the first one and regard the rest of the merged cells as null

ASP.NET Backgroundworkers for spreadsheet creation: multiple ones interfering with each other?

I am writing an ASP.NET application in which i need to create multiple excel reports. the report creation is pretty time-consuming (up to ten seconds for each) so i am using backgroundworkers to create them simultaneously.
My code looks a bit like this:
if (condition1)
{
excel_file_name = "TRANSFER";
BackgroundWorker worker_t = new BackgroundWorker();
worker_t.DoWork += new DoWorkEventHandler(DoWork);
worker_t.WorkerReportsProgress = false;
worker_t.WorkerSupportsCancellation = true;
worker_t.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(WorkerCompleted);
worker_t.RunWorkerAsync(excel_file_name);
}
if (Condition2)
{
excel_file_name = "NEFT";
BackgroundWorker worker_n = new BackgroundWorker();
worker_n.DoWork += new DoWorkEventHandler(DoWork);
worker_n.WorkerReportsProgress = false;
worker_n.WorkerSupportsCancellation = true;
worker_n.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(WorkerCompleted);
worker_n.RunWorkerAsync(excel_file_name);
}
there are more conditions but i haven't written them, since they are all similar. the only difference is the Excel_File_Name
the DoWork even then calls a class to create the excel files with the given name.
When condition1 and condition2 are both true, Here is the issue:
1. if i run this slowly using breakpoints during debugging, both files (TRANSFER and NEFT) are created.
2. if, however, i run it without breakpoints like a normal application, only the last file (NEFT in this example) is created.
What can be the issue?
Thanks
PS: For further information, here is the important code from the class that creates the excel file:
private static string placeDataInTemplate(string destFilePath, DataRow dr, bool isCoverLetter)
{
int loop = 0;
ExcelNamespace.Application excelApplication = new ExcelNamespace.Application();
ExcelNamespace.Workbook workbook = excelApplication.Workbooks.Open(destFilePath, 0, false, 5,
"", "", true, ExcelNamespace.XlPlatform.xlWindows, "\t", false, false, 0, true, true, false);
ExcelNamespace.Worksheet workSheet = (ExcelNamespace.Worksheet)workbook.Sheets[sheet_no];
try
{
string value;
string replicate;
string replicate_end;
// get data for Place Holders
sDataTable dtPlaceHolderData = getPlaceHolderData(dr);
//make Display Alerts False
excelApplication.DisplayAlerts = false;
if (dtPlaceHolderData != null && dtPlaceHolderData.Rows.Count > 0)
{
int rowCntDt = 0; //Which row will be used for data?
int i = 1;
Excel.Range Find = (ExcelNamespace.Range)workSheet.Cells.Find("#",
(ExcelNamespace.Range)workSheet.Cells[1, 1],
Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows,
Excel.XlSearchDirection.xlNext,
false,
false,
Missing.Value);
while (Find != null && loop <= 200)
{
loop++;
value = Find.Value2.ToString();
if (condition)
//VERY long if...else if
}
string approveDirPath = destFilePath.Replace(Path.GetFileName(destFilePath), string.Empty);
workbook.Close(true, destFilePath, Type.Missing);
excelApplication.Quit();
string filepath = destFilePath.Split('-')[0];
string approval_id = dr[0].ToString();
return destFilePath;
}
return string.Empty;
}
catch (Exception ex)
{
//do something
}
finally
{
//release resources
}
NOTE: I have removed a lot of needless code. I can paste it if needed. Thank you
Most likely cause is some shared state between two threads - shared state may include excel application and workbooks. So you need to inspect your code for the same.
On the side note, instead of using Excel Automation to generate excel files, you may consider using some in-process library which would be perhaps more scalable and void of such issues. Have a look at one such free basic library at code project

Resources