I am trying to append to an existing worksheet using the code below but it overwrites the existing data instead of appending.
string filePath = "c:\\CompareReport.xlsx";
FileInfo fi = new FileInfo(filePath);
using (var package = new ExcelPackage(fi)) {
var currentSheet = package.Workbook.Worksheets;
var sheet = currentSheet.First();
sheet.Row(sheet.Dimension.End.Row);
sheet.Cells.LoadFromCollection(myData, true);
package.SaveAs(fi);
}
Related
I have two azure storage container
Container A
Container B
Container A will have an excel file which I will need to download it on the fly & do some modifications for some business logic.
Once modification is done ,the excel file should be uploaded in Container B.
Example:
Excel in Container A have a column named "ABC" which I will have to replace it with "XYZ"
So a new excel with XYZ content should be saved in Container B.
Any help would be appreciated.
Regards,
Regarding the issue, please refer to the following code
Upload
string accountName = "jimtestdiag924";
string accountKey = "uxz4AtF0*********yDSZ7Q+A==";
var credential = new StorageSharedKeyCredential(accountName, accountKey);
string url = string.Format("https://{0}.blob.core.windows.net/", accountName);
var blobServiceClient =new BlobServiceClient(new Uri(url), credential);
var containerClient = blobServiceClient.GetBlobContainerClient("testupload");
var blobClient =containerClient.GetBlobClient("test.xlsx");
BlobDownloadInfo download = await blobClient.DownloadAsync();
using (var ms = new MemoryStream()) {
await download.Content.CopyToAsync(ms);
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(ms, true))
{
// Access the main Workbook part, which contains all references.
WorkbookPart workbookPart = spreadSheet.WorkbookPart;
// get sheet by name
Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet1").FirstOrDefault();
// get worksheetpart by sheet id
WorksheetPart worksheetPart = workbookPart.GetPartById(sheet.Id.Value) as WorksheetPart;
Cell cell = GetCell(worksheetPart.Worksheet, "B", 4);
cell.CellValue = new CellValue("10");
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
// Save the worksheet.
worksheetPart.Worksheet.Save();
// for recacluation of formula
spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
}
// upload file to another contanier
ms.Position = 0;
containerClient = blobServiceClient.GetBlobContainerClient("test");
await containerClient.CreateIfNotExistsAsync();
blobClient = containerClient.GetBlobClient("test.xlsx");
await blobClient.UploadAsync(ms);
download.Content.Close();
}
Download
string accountName = "jimtestdiag924";
string accountKey = "uxz4AtF0*********yDSZ7Q+A==";
var credential = new StorageSharedKeyCredential(accountName, accountKey);
string url = string.Format("https://{0}.blob.core.windows.net/", accountName);
var blobServiceClient =new BlobServiceClient(new Uri(url), credential);
var containerClient = blobServiceClient.GetBlobContainerClient("test");
var blobClient = containerClient.GetBlobClient("test.xlsx");
BlobDownloadInfo download = await blobClient.DownloadAsync();
using (var file = File.Create(#"D:\test.xlsx")) {
await download.Content.CopyToAsync(file);
download.Content.Close();
}
The original excel file
The new excel file
so I have a program that extracts a csvTable from a csvFile structured like:
string Output_Path = Output_Path_value(output_path);
string Conversion_Logic = Conversion_Logic_value(conversion_logic); //Conversion_Logic
var textwriter = Console.Out;
using (var csvWriter = new CsvWriter(textwriter, CultureInfo.InvariantCulture))
using (var writer = new StreamWriter(Output_Path + #"\" + Conversion_Logic + "_" + fileName))
using (var csv_write = new CsvWriter(writer, CultureInfo.InvariantCulture))
using (var reader = new StringReader(sb.ToString()))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.MissingFieldFound = null;
csv.Configuration.IgnoreBlankLines = true;
csv.Configuration.RegisterClassMap<CsvTable_Headers_Map>();
csv.Read();
csv.ReadHeader();
csv.Configuration.ShouldSkipRecord = row => row[0].StartsWith("__") || row[0].StartsWith("T");
var records = csv.GetRecords<CsvTable_Headers>();
csv_write.WriteRecords(records);
//csvWriter.WriteRecords(records);// this has to be before or not exist for file to save correctly
}
The purpose of this code is to save the CsvTable to a seperate file, and then save it to the that specified output path. Everything here works, however something happens is that it also saves that file to the Debug folder, when it shouldnt. How should I structure it so that it does not save it there, and only to the output path?
I am also using the open source library, csvHelper.
I am trying to add new worksheet into existing workbook, code runs fine without any error. But changes are not being updated to the excel file.
Here is my code
string path = "C:\\TestFileSave\\ABC.xlsx";
FileInfo filePath = new FileInfo(path);
if (File.Exists(path))
{
using(ExcelPackage p = new ExcelPackage())
{
using(stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
p.Load(stream);
ExcelWorksheet ws = p.Workbook.Worksheets.Add(wsName + wsNumber.ToString());
ws.Cells[1, 1].Value = wsName;
ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
ws.Cells[1, 1].Style.Font.Bold = true;
p.Save();
}
}
}
The stream object is not tied to the package. The only relationship is it copies its bytes in your call to Load afterwards they are separate.
You do not need to even use a stream - better to let the package handle it on its own like this:
var fileinfo = new FileInfo(path);
if (fileinfo.Exists)
{
using (ExcelPackage p = new ExcelPackage(fileinfo))
{
//using (stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
//p.Load(stream);
ExcelWorksheet ws = p.Workbook.Worksheets.Add(wsName + wsNumber.ToString());
ws.Cells[1, 1].Value = wsName;
ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
ws.Cells[1, 1].Style.Font.Bold = true;
p.Save();
}
}
}
Here I have shown to write data into exiting excel file by creating a new sheet in same file. To answer your question: try using last two lines File.WriteAllBytes instead of p.Save().
string strfilepath = "C:\\Users\\m\\Desktop\\Employeedata.xlsx";
using (ExcelPackage p = new ExcelPackage())
{
using (FileStream stream = new FileStream(strfilepath, FileMode.Open))
{
p.Load(stream);
//deleting worksheet if already present in excel file
var wk = p.Workbook.Worksheets.SingleOrDefault(x => x.Name == "Hola");
if (wk != null) { p.Workbook.Worksheets.Delete(wk); }
p.Workbook.Worksheets.Add("Hola");
p.Workbook.Worksheets.MoveToEnd("Hola");
ExcelWorksheet worksheet = p.Workbook.Worksheets[p.Workbook.Worksheets.Count];
worksheet.InsertRow(5, 2);
worksheet.Cells["A9"].LoadFromDataTable(dt1, true);
// Inserting values in the 5th row
worksheet.Cells["A5"].Value = "12010";
worksheet.Cells["B5"].Value = "Drill";
worksheet.Cells["C5"].Value = 20;
worksheet.Cells["D5"].Value = 8;
// Inserting values in the 6th row
worksheet.Cells["A6"].Value = "12011";
worksheet.Cells["B6"].Value = "Crowbar";
worksheet.Cells["C6"].Value = 7;
worksheet.Cells["D6"].Value = 23.48;
}
//p.Save() ;
Byte[] bin = p.GetAsByteArray();
File.WriteAllBytes(#"C:\Users\m\Desktop\Employeedata.xlsx", bin);
}
I originally got the error code "A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT)) " from using this, but later learned that it was because the existing Excel file that I wanted to modify wasn't fully MS-Excel format compliant. I created thee original excel file in Open office as an .xls file, but EPPlus was not able to read it. When I regenerated this original excel file in Online Excel, everything worked fine.
I have a text file with some information and I convert it to ExcelPackage Object using EPPlus, now I want to know if there is a way to open this object with excel without saving it to a local file? if is not possible can I use a temp directory to save it into a file, and then open it?
If you are talking about a windows app, you could just use something like System.IO.Path.GetTempPath(). You can get more info from here:
How to get temporary folder for current user
So, something like this:
[TestMethod]
public void TempFolderTest()
{
var path = Path.Combine(Path.GetTempPath(), "temp.xlsx");
var tempfile = new FileInfo(path);
if (tempfile.Exists)
tempfile.Delete();
//Save the file
using (var pck = new ExcelPackage(tempfile))
{
var ws = pck.Workbook.Worksheets.Add("Demo");
ws.Cells[1, 2].Value = "Excel Test";
pck.Save();
}
//open the file
Process.Start(tempfile.FullName);
}
if you are talking web you shouldn't need to save it all, just send it via Response:
using (ExcelPackage pck = new ExcelPackage())
{
var ws = pck.Workbook.Worksheets.Add("Demo");
ws.Cells[1, 2].Value = "Excel Test";
var fileBytes = pck.GetAsByteArray();
Response.Clear();
Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
Response.AppendHeader("Content-Disposition",
String.Format("attachment; filename=\"{0}\"; size={1}; creation-date={2}; modification-date={2}; read-date={2}"
, "temp.xlsx"
, fileBytes.Length
, DateTime.Now.ToString("R"))
);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(fileBytes);
Response.End();
}
I am exporting excel file from database using following method.But i have one problem when i am exporting excel file than it is automatically downloading to download folder and i don't want this to be happen,I want my excel file to be downloaded in my project folder
var formsection = from fs in db.FormSections
join form in Form on fs.FormId equals form.FormId
select fs;
XLWorkbook wb = new XLWorkbook();
string sheetName = "ARTICLE"; //Give name for export file.
var Fs = wb.Worksheets.Add("FORMSECTION");
Fs.Cell(2, 1).InsertTable(formsection.ToList());// assign list here.
HttpContext.Response.Clear();
HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Response.AddHeader("content-disposition", String.Format(#"attachment;filename={0}.xlsx", sheetName.Replace(" ", "_")));
var filePath = Path.Combine(Server.MapPath("~/Content"));
using (MemoryStream memoryStream = new MemoryStream())
{
wb.SaveAs(memoryStream);
memoryStream.WriteTo(HttpContext.Response.OutputStream);
memoryStream.Close();
}
HttpContext.Response.End();
Here is an example that converts a datatable to a .csv file and save the file to the folder of your project for me its store the file in Domestic folder as the path given by me.
private void test(DataTable dt1) {
string csv = string.Empty;
foreach (DataColumn column in dt1.Columns)
{
//Add the Header row for CSV file.
csv += column.ColumnName + ',';
}
//Add new line.
csv += "\r\n";
foreach (DataRow row in dt1.Rows)
{
foreach (DataColumn column in dt1.Columns)
{
//Add the Data rows.
csv += row[column.ColumnName].ToString().Replace(",", ";") +',';
}
//Add new line.
csv += "\r\n";
}
string datetime = Convert.ToString(DateTime.Today.ToString("dd-MM-yyyy")).Trim();
string filepath = "C:\\Users\\Prateek\\Desktop\\MMR New 27-07 -\\Domestic\\";
string filename= #"BILLING_BOOK_NO" + "_4005" + "_"+datetime+".CSV";
string combinepath = filepath + filename;
System.IO.File.WriteAllText(combinepath,csv);
}`
You need to write the excel to your server first.
wb.SaveAs(filePath);
//encrypt the file
Encrypt(filePath);
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (MemoryStream memoryStream = new MemoryStream())
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
memoryStream.Write(bytes, 0, (int)file.Length);
memoryStream.WriteTo(HttpContext.Response.OutputStream);
}
}
use code as below
public void ImportXLX()
{
string filePath = string.Format("{0}/{1}", Server.MapPath("~/Content/UploadedFolder"), #"C:\Users\Vipin\Desktop\Sheets\MyXL6.xlsx");
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
Request.Files["xlsFile"].SaveAs(filePath);
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
}