I have a excel file which contains sheet named Data. This sheet already contains few data. I need to open this file and add more data in it. I have tried searching about this but everyone is just creating new sheets in their workbook. I need to update the current sheet. Below is my code:
MemoryStream ms = new MemoryStream();
using (FileStream fs = File.OpenRead(#"Path\File.xlsx")
using (ExcelPackage excelPackage = new ExcelPackage(fs))
{
ExcelWorkbook excelWorkBook = excelPackage.Workbook;
ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
excelWorksheet.Cells[1, 1].Value = "Test";
excelWorksheet.Cells[3, 2].Value = "Test2";
excelWorksheet.Cells[3, 3].Value = "Test3";
excelPackage.SaveAs(ms);
}
but it didnt update the sheet. I do not what I am doing wrong. Can anyone help me please. Thanks
You are doing this:
Create a MemoryStream object in memory to store binary data.
Open file and read it into the MemoryStream object.
Create an ExcelPackage object based on the data in the MemoryStream object.
Make changes to the spreadsheet.
Save changes back to the MemoryStream object.
That's why the spreadsheet file does not get updated.
Use FileInfo and open the file directly with ExcelPackage:
// using System.IO;
FileInfo file = new FileInfo(#"Path\File.xlsx");
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
ExcelWorkbook excelWorkBook = excelPackage.Workbook;
ExcelWorksheet excelWorksheet = excelWorkBook.Worksheets.First();
excelWorksheet.Cells[1, 1].Value = "Test";
excelWorksheet.Cells[3, 2].Value = "Test2";
excelWorksheet.Cells[3, 3].Value = "Test3";
excelPackage.Save();
}
Try this
var file = new FileInfo(path);
if (file .Exists)
{
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
ExcelWorksheet ws = excelPackage.Workbook.Worksheets.First();
ws.Cells[1,1].Value = "Test1";
excelPackage.Save();
}
}
Related
I want to load an excel file into XLWorkbook
using var workbook = new XLWorkbook("iFormFile needs to go here");
var ws = workbook.Worksheet(1);
There can be better answers than this, but this should work.
using var workbook = new XLWorkbook(iFormFile.OpenReadStream());
var ws = workbook.Worksheet(1);
I am trying to read a excel file using
using (var stream = new FileStream(filepath, FileMode.Append,FileAccess.Write))
{
ExcelWorksheet worksheet;
using (ExcelPackage pck = new ExcelPackage(stream))
actually I am trying to add a worksheet in existing excel file so that's why I am using append , and when I used filemode.create then it is overtiring my existing tab with the new one so what should I do?
Just use the following code and it will add a new worksheet to your current file:
FileInfo newFile = new FileInfo("YourFile.xlsx");
using (ExcelPackage p = new ExcelPackage(newFile))
{
p.Workbook.Worksheets.Add("YourNewSheet");
p.Save();
}
You can do the same actions easily with Microsoft open xml
https://learn.microsoft.com/en-us/office/open-xml/how-to-get-worksheet-information-from-a-package
I have problem with export dataset into xlsx. My code:
DataSet freeTable = new DataSet();
freeTable = FoxDal.Instance.freeTable;
XLWorkbook wb = new XLWorkbook();
wb.Worksheets.Add(freeTable.Tables[0], "sheet1");
wb.SaveAs(#"E:\test5.xlsx");
While running code VS give's me prompt ( asking me where is XLReentrantEnumerableSet.cs) if i dont choose it VS throw exception System.OutOfMemoryException. Im using ClosedXML. There are some attached libraries for this framework? With small tables my code working corectly.
I change framework to EPPlus and now i can convert large files.
My new code:
DataSet freeTable = new DataSet();
freeTable = FoxDal.Instance.freeTable;
FileInfo fileInfo = new FileInfo(#"E:\Test.xlsx");
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("table");
ws.Cells["A1"].LoadFromDataTable(freeTable.Tables[0], true);
pck.SaveAs(fileInfo);
}
I need to copy a sheet from one workbook to another. I am trying with the below code, but it is not working:
ISheet newSheet = wb.GetSheetAt(0).CopySheet("WeeklyReport");
string filePath = "billing_template2.xlsx";
XSSFWorkbook billingWorkbook;
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
billingWorkbook = new XSSFWorkbook(fs);
}
billingWorkbook.Add(newSheet);
where wb is the source workbook and billingWorkbook is my destination workbook.
Note: My destination workbook already had a sheet. I need to add the copied sheet after this one.
I am creating an Excel using EPPlus and Datatable.Now as per my requirement i have to save this into my local system directory but i am not able to get how to achieve this ..
The path where i have to save is C://Reports//excel.xls
Here is my code
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(reportName);
ws.Cells["A1"].LoadFromDataTable(DT, true);
pck.Save();
}
Please help me ..
FileInfo newFile = new FileInfo(#"C:\Reports\excel.xls");
if (newFile.Exists)
{
newFile.Delete(); // ensures we create a new workbook
newFile = new FileInfo(#"C:\Reports\excel.xls");
}
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(reportName);
ws.Cells["A1"].LoadFromDataTable(DT, true);
pck.Save();
}