updating a excel document does not keeps what the document had
Am using this code to update an excel document, it saves the new data but the old data is no longer displayed, can you please help i just want to update the excel document.
public void addRow(string path,int row, string req)
{
var f1 = path + "Reqs.xlsx";
FileInfo file = new FileInfo(f1);
var package = new ExcelPackage(file);
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
worksheet.Cells[row, 1].Value = req;
package.Save();
}
c# epplus
add a comment |
Am using this code to update an excel document, it saves the new data but the old data is no longer displayed, can you please help i just want to update the excel document.
public void addRow(string path,int row, string req)
{
var f1 = path + "Reqs.xlsx";
FileInfo file = new FileInfo(f1);
var package = new ExcelPackage(file);
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
worksheet.Cells[row, 1].Value = req;
package.Save();
}
c# epplus
It would be awesome if you could provide a Minimal, Complete, and Verifiable example.
– mjwills
Nov 13 '18 at 7:09
add a comment |
Am using this code to update an excel document, it saves the new data but the old data is no longer displayed, can you please help i just want to update the excel document.
public void addRow(string path,int row, string req)
{
var f1 = path + "Reqs.xlsx";
FileInfo file = new FileInfo(f1);
var package = new ExcelPackage(file);
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
worksheet.Cells[row, 1].Value = req;
package.Save();
}
c# epplus
Am using this code to update an excel document, it saves the new data but the old data is no longer displayed, can you please help i just want to update the excel document.
public void addRow(string path,int row, string req)
{
var f1 = path + "Reqs.xlsx";
FileInfo file = new FileInfo(f1);
var package = new ExcelPackage(file);
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
worksheet.Cells[row, 1].Value = req;
package.Save();
}
c# epplus
c# epplus
edited Nov 13 '18 at 6:53
Thilina Nakkawita
781927
781927
asked Nov 13 '18 at 6:23
Josue Augusto Ramirez
1
1
It would be awesome if you could provide a Minimal, Complete, and Verifiable example.
– mjwills
Nov 13 '18 at 7:09
add a comment |
It would be awesome if you could provide a Minimal, Complete, and Verifiable example.
– mjwills
Nov 13 '18 at 7:09
It would be awesome if you could provide a Minimal, Complete, and Verifiable example.
– mjwills
Nov 13 '18 at 7:09
It would be awesome if you could provide a Minimal, Complete, and Verifiable example.
– mjwills
Nov 13 '18 at 7:09
add a comment |
1 Answer
1
active
oldest
votes
Here I have taken Emp list collection do the add the recodes.
public class Emp
{public int ID { get; set; }
public string Name { get; set; }
}
private static void AddNewRowsToExcelFile()
{
IList<Emp> empList = new List<Emp>()
{
new Emp(){ ID=1003, Name="Name1"},
new Emp(){ ID=1004, Name="Name2"},
new Emp(){ ID=1005, Name="Name3"}
};
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5,
"", "", false,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int rowNumber = xlRange.Rows.Count + 1;
foreach (Emp emp in empList)
{
xlWorkSheet.Cells[rowNumber, 1] = emp.ID;
xlWorkSheet.Cells[rowNumber, 2] = emp.Name;
rowNumber++;
}
// Disable file override confirmaton message
xlApp.DisplayAlerts = false;
xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlOpenXMLWorkbook,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value,
Missing.Value,
Missing.Value, Missing.Value);
xlWorkBook.Close();
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("nRecords Added successfully...");
Console.BackgroundColor = ConsoleColor.Black;
}
Once the records are added it will save the file without asking confirmation to override the file.
In my code what part am missing that is overrifing everything? What part i miss to add to not consider the information it already had.
– Josue Augusto Ramirez
Nov 14 '18 at 0:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274979%2fupdating-a-excel-document-does-not-keeps-what-the-document-had%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here I have taken Emp list collection do the add the recodes.
public class Emp
{public int ID { get; set; }
public string Name { get; set; }
}
private static void AddNewRowsToExcelFile()
{
IList<Emp> empList = new List<Emp>()
{
new Emp(){ ID=1003, Name="Name1"},
new Emp(){ ID=1004, Name="Name2"},
new Emp(){ ID=1005, Name="Name3"}
};
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5,
"", "", false,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int rowNumber = xlRange.Rows.Count + 1;
foreach (Emp emp in empList)
{
xlWorkSheet.Cells[rowNumber, 1] = emp.ID;
xlWorkSheet.Cells[rowNumber, 2] = emp.Name;
rowNumber++;
}
// Disable file override confirmaton message
xlApp.DisplayAlerts = false;
xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlOpenXMLWorkbook,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value,
Missing.Value,
Missing.Value, Missing.Value);
xlWorkBook.Close();
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("nRecords Added successfully...");
Console.BackgroundColor = ConsoleColor.Black;
}
Once the records are added it will save the file without asking confirmation to override the file.
In my code what part am missing that is overrifing everything? What part i miss to add to not consider the information it already had.
– Josue Augusto Ramirez
Nov 14 '18 at 0:32
add a comment |
Here I have taken Emp list collection do the add the recodes.
public class Emp
{public int ID { get; set; }
public string Name { get; set; }
}
private static void AddNewRowsToExcelFile()
{
IList<Emp> empList = new List<Emp>()
{
new Emp(){ ID=1003, Name="Name1"},
new Emp(){ ID=1004, Name="Name2"},
new Emp(){ ID=1005, Name="Name3"}
};
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5,
"", "", false,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int rowNumber = xlRange.Rows.Count + 1;
foreach (Emp emp in empList)
{
xlWorkSheet.Cells[rowNumber, 1] = emp.ID;
xlWorkSheet.Cells[rowNumber, 2] = emp.Name;
rowNumber++;
}
// Disable file override confirmaton message
xlApp.DisplayAlerts = false;
xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlOpenXMLWorkbook,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value,
Missing.Value,
Missing.Value, Missing.Value);
xlWorkBook.Close();
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("nRecords Added successfully...");
Console.BackgroundColor = ConsoleColor.Black;
}
Once the records are added it will save the file without asking confirmation to override the file.
In my code what part am missing that is overrifing everything? What part i miss to add to not consider the information it already had.
– Josue Augusto Ramirez
Nov 14 '18 at 0:32
add a comment |
Here I have taken Emp list collection do the add the recodes.
public class Emp
{public int ID { get; set; }
public string Name { get; set; }
}
private static void AddNewRowsToExcelFile()
{
IList<Emp> empList = new List<Emp>()
{
new Emp(){ ID=1003, Name="Name1"},
new Emp(){ ID=1004, Name="Name2"},
new Emp(){ ID=1005, Name="Name3"}
};
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5,
"", "", false,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int rowNumber = xlRange.Rows.Count + 1;
foreach (Emp emp in empList)
{
xlWorkSheet.Cells[rowNumber, 1] = emp.ID;
xlWorkSheet.Cells[rowNumber, 2] = emp.Name;
rowNumber++;
}
// Disable file override confirmaton message
xlApp.DisplayAlerts = false;
xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlOpenXMLWorkbook,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value,
Missing.Value,
Missing.Value, Missing.Value);
xlWorkBook.Close();
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("nRecords Added successfully...");
Console.BackgroundColor = ConsoleColor.Black;
}
Once the records are added it will save the file without asking confirmation to override the file.
Here I have taken Emp list collection do the add the recodes.
public class Emp
{public int ID { get; set; }
public string Name { get; set; }
}
private static void AddNewRowsToExcelFile()
{
IList<Emp> empList = new List<Emp>()
{
new Emp(){ ID=1003, Name="Name1"},
new Emp(){ ID=1004, Name="Name2"},
new Emp(){ ID=1005, Name="Name3"}
};
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5,
"", "", false,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true,
false, 0, true, false, false);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range xlRange = xlWorkSheet.UsedRange;
int rowNumber = xlRange.Rows.Count + 1;
foreach (Emp emp in empList)
{
xlWorkSheet.Cells[rowNumber, 1] = emp.ID;
xlWorkSheet.Cells[rowNumber, 2] = emp.Name;
rowNumber++;
}
// Disable file override confirmaton message
xlApp.DisplayAlerts = false;
xlWorkBook.SaveAs(filePath, Excel.XlFileFormat.xlOpenXMLWorkbook,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value,
Missing.Value,
Missing.Value, Missing.Value);
xlWorkBook.Close();
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("nRecords Added successfully...");
Console.BackgroundColor = ConsoleColor.Black;
}
Once the records are added it will save the file without asking confirmation to override the file.
answered Nov 13 '18 at 7:04
Vivek Mishra
12
12
In my code what part am missing that is overrifing everything? What part i miss to add to not consider the information it already had.
– Josue Augusto Ramirez
Nov 14 '18 at 0:32
add a comment |
In my code what part am missing that is overrifing everything? What part i miss to add to not consider the information it already had.
– Josue Augusto Ramirez
Nov 14 '18 at 0:32
In my code what part am missing that is overrifing everything? What part i miss to add to not consider the information it already had.
– Josue Augusto Ramirez
Nov 14 '18 at 0:32
In my code what part am missing that is overrifing everything? What part i miss to add to not consider the information it already had.
– Josue Augusto Ramirez
Nov 14 '18 at 0:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274979%2fupdating-a-excel-document-does-not-keeps-what-the-document-had%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It would be awesome if you could provide a Minimal, Complete, and Verifiable example.
– mjwills
Nov 13 '18 at 7:09