How to read from Excel and replace certain values in a .txt file
I am new in c#. I have just some basic knowledge like copy and paste. That means I am not good in c#. And sorry for my english. Now to my problem. I have a notepad.txt file. It contains some parameters with values which I get from an other program.
Description of this .txt File:
[Unit optional] name_of_parameter = value
notepad.txt:
[mm]p0=45
[mm]param1=36.42199010819
[mm]param2=56
param9=56
[degrees]p3=453
[degrees]p4=134
[mm]p5=56
p6=1
p7=1
There is no chance to get another style of this output (.txt File).
In addition to this I have an excel sheet with random rows and columns. In the first row are the name_of_parameter like -> param1, p0.....
So I want to replace for example the value of param2 (in my .txt file) with the value of param2 from my excel sheet. I want to do this with all name_of_parameters which exists in both files in a loop because my excel sheet has more than one row. Conclusion i want to manipulate the notepad.txt file.
screenshot
my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication3
{
class Program
{
static void Main(string args)
{
string lines = System.IO.File.ReadAllLines(@"C:UsersZ003DUKJDesktopnotepad.txt");
int stop = 1;
}
}
}
If somebody has some tipps advices for me thank you.
c# excel
add a comment |
I am new in c#. I have just some basic knowledge like copy and paste. That means I am not good in c#. And sorry for my english. Now to my problem. I have a notepad.txt file. It contains some parameters with values which I get from an other program.
Description of this .txt File:
[Unit optional] name_of_parameter = value
notepad.txt:
[mm]p0=45
[mm]param1=36.42199010819
[mm]param2=56
param9=56
[degrees]p3=453
[degrees]p4=134
[mm]p5=56
p6=1
p7=1
There is no chance to get another style of this output (.txt File).
In addition to this I have an excel sheet with random rows and columns. In the first row are the name_of_parameter like -> param1, p0.....
So I want to replace for example the value of param2 (in my .txt file) with the value of param2 from my excel sheet. I want to do this with all name_of_parameters which exists in both files in a loop because my excel sheet has more than one row. Conclusion i want to manipulate the notepad.txt file.
screenshot
my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication3
{
class Program
{
static void Main(string args)
{
string lines = System.IO.File.ReadAllLines(@"C:UsersZ003DUKJDesktopnotepad.txt");
int stop = 1;
}
}
}
If somebody has some tipps advices for me thank you.
c# excel
add a comment |
I am new in c#. I have just some basic knowledge like copy and paste. That means I am not good in c#. And sorry for my english. Now to my problem. I have a notepad.txt file. It contains some parameters with values which I get from an other program.
Description of this .txt File:
[Unit optional] name_of_parameter = value
notepad.txt:
[mm]p0=45
[mm]param1=36.42199010819
[mm]param2=56
param9=56
[degrees]p3=453
[degrees]p4=134
[mm]p5=56
p6=1
p7=1
There is no chance to get another style of this output (.txt File).
In addition to this I have an excel sheet with random rows and columns. In the first row are the name_of_parameter like -> param1, p0.....
So I want to replace for example the value of param2 (in my .txt file) with the value of param2 from my excel sheet. I want to do this with all name_of_parameters which exists in both files in a loop because my excel sheet has more than one row. Conclusion i want to manipulate the notepad.txt file.
screenshot
my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication3
{
class Program
{
static void Main(string args)
{
string lines = System.IO.File.ReadAllLines(@"C:UsersZ003DUKJDesktopnotepad.txt");
int stop = 1;
}
}
}
If somebody has some tipps advices for me thank you.
c# excel
I am new in c#. I have just some basic knowledge like copy and paste. That means I am not good in c#. And sorry for my english. Now to my problem. I have a notepad.txt file. It contains some parameters with values which I get from an other program.
Description of this .txt File:
[Unit optional] name_of_parameter = value
notepad.txt:
[mm]p0=45
[mm]param1=36.42199010819
[mm]param2=56
param9=56
[degrees]p3=453
[degrees]p4=134
[mm]p5=56
p6=1
p7=1
There is no chance to get another style of this output (.txt File).
In addition to this I have an excel sheet with random rows and columns. In the first row are the name_of_parameter like -> param1, p0.....
So I want to replace for example the value of param2 (in my .txt file) with the value of param2 from my excel sheet. I want to do this with all name_of_parameters which exists in both files in a loop because my excel sheet has more than one row. Conclusion i want to manipulate the notepad.txt file.
screenshot
my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication3
{
class Program
{
static void Main(string args)
{
string lines = System.IO.File.ReadAllLines(@"C:UsersZ003DUKJDesktopnotepad.txt");
int stop = 1;
}
}
}
If somebody has some tipps advices for me thank you.
c# excel
c# excel
edited Nov 23 '18 at 10:05
Uwe Keim
27.7k32134216
27.7k32134216
asked Nov 23 '18 at 9:10
Lin404Lin404
45
45
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
see the link.
How to read single Excel cell value
Note that your code had pointed to correct worksheet. You will get access to each cell from the code. You can manipulate the value
add a comment |
Following the version excel you have, this nuget package does the job for you
see link
you have some interesting samples
add a comment |
The best I can think is:
- Read all lines, like you did in your code
- Parse the lines thatcontains
paramNumto objcets and put these to aDictionary<string, double>. - Iterate through your Keys in Dictionary
and search* for that cell in your excel table associated to your
Key. - If you found the cell that contains the paramter, read it's value
regarding it's location eg.: if the param in
xlWorkSheet.Cells[row,"A"]then in your excel file the value is in
xlWorkSheet.Cells[row-1, "A"]. - Then replace cell value with your dictionary item's value
How to Read xls file:
var excelApp = new Excel.Application();
var xlWorkBook = excelApp.Workbooks.Open(fileName);
var ws = (_Worksheet)wb.Worksheets[1];
Search for cell:
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.Range rangeOfParams = Application.get_Range("A1", "E5");
// You should specify all these parameters every time you call this method,
// since they can be overridden in the user interface.
currentFind = rangeOfParams .Find("param1", missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
missing, missing);
while(currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1)
== firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
{
break;
}
currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
currentFind.Font.Bold = true;
currentFind = rangeOfParams.FindNext(currentFind);
}
Replace value of cell:
ws.Cells[2, "A"] = SomeValue;
ws.Cells[2, "E"] = OtherValue;
MSDN Microsoft Excel Solutions
thank you so much!!! i will check the code if understand it. maybe i have a question
– Lin404
Nov 23 '18 at 9:59
Let me know if you have question. This is not the full code, but a path where you can start.
– koviroli
Nov 23 '18 at 10:02
@kovirli can you send me a full code? I mean a working code? I combine this parts with my code so I have some errors like ".. namespace "_Application could not be found (are you missing a using directive or an assembly?
– Lin404
Nov 23 '18 at 12:02
Updated my code, sorre for that, I wrongly pasted that _Application and other names.
– koviroli
Nov 23 '18 at 12:12
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%2f53443594%2fhow-to-read-from-excel-and-replace-certain-values-in-a-txt-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
see the link.
How to read single Excel cell value
Note that your code had pointed to correct worksheet. You will get access to each cell from the code. You can manipulate the value
add a comment |
see the link.
How to read single Excel cell value
Note that your code had pointed to correct worksheet. You will get access to each cell from the code. You can manipulate the value
add a comment |
see the link.
How to read single Excel cell value
Note that your code had pointed to correct worksheet. You will get access to each cell from the code. You can manipulate the value
see the link.
How to read single Excel cell value
Note that your code had pointed to correct worksheet. You will get access to each cell from the code. You can manipulate the value
answered Nov 23 '18 at 9:22
Agustus CodesAgustus Codes
3019
3019
add a comment |
add a comment |
Following the version excel you have, this nuget package does the job for you
see link
you have some interesting samples
add a comment |
Following the version excel you have, this nuget package does the job for you
see link
you have some interesting samples
add a comment |
Following the version excel you have, this nuget package does the job for you
see link
you have some interesting samples
Following the version excel you have, this nuget package does the job for you
see link
you have some interesting samples
answered Nov 23 '18 at 10:11
FrenchyFrenchy
1,7802515
1,7802515
add a comment |
add a comment |
The best I can think is:
- Read all lines, like you did in your code
- Parse the lines thatcontains
paramNumto objcets and put these to aDictionary<string, double>. - Iterate through your Keys in Dictionary
and search* for that cell in your excel table associated to your
Key. - If you found the cell that contains the paramter, read it's value
regarding it's location eg.: if the param in
xlWorkSheet.Cells[row,"A"]then in your excel file the value is in
xlWorkSheet.Cells[row-1, "A"]. - Then replace cell value with your dictionary item's value
How to Read xls file:
var excelApp = new Excel.Application();
var xlWorkBook = excelApp.Workbooks.Open(fileName);
var ws = (_Worksheet)wb.Worksheets[1];
Search for cell:
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.Range rangeOfParams = Application.get_Range("A1", "E5");
// You should specify all these parameters every time you call this method,
// since they can be overridden in the user interface.
currentFind = rangeOfParams .Find("param1", missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
missing, missing);
while(currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1)
== firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
{
break;
}
currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
currentFind.Font.Bold = true;
currentFind = rangeOfParams.FindNext(currentFind);
}
Replace value of cell:
ws.Cells[2, "A"] = SomeValue;
ws.Cells[2, "E"] = OtherValue;
MSDN Microsoft Excel Solutions
thank you so much!!! i will check the code if understand it. maybe i have a question
– Lin404
Nov 23 '18 at 9:59
Let me know if you have question. This is not the full code, but a path where you can start.
– koviroli
Nov 23 '18 at 10:02
@kovirli can you send me a full code? I mean a working code? I combine this parts with my code so I have some errors like ".. namespace "_Application could not be found (are you missing a using directive or an assembly?
– Lin404
Nov 23 '18 at 12:02
Updated my code, sorre for that, I wrongly pasted that _Application and other names.
– koviroli
Nov 23 '18 at 12:12
add a comment |
The best I can think is:
- Read all lines, like you did in your code
- Parse the lines thatcontains
paramNumto objcets and put these to aDictionary<string, double>. - Iterate through your Keys in Dictionary
and search* for that cell in your excel table associated to your
Key. - If you found the cell that contains the paramter, read it's value
regarding it's location eg.: if the param in
xlWorkSheet.Cells[row,"A"]then in your excel file the value is in
xlWorkSheet.Cells[row-1, "A"]. - Then replace cell value with your dictionary item's value
How to Read xls file:
var excelApp = new Excel.Application();
var xlWorkBook = excelApp.Workbooks.Open(fileName);
var ws = (_Worksheet)wb.Worksheets[1];
Search for cell:
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.Range rangeOfParams = Application.get_Range("A1", "E5");
// You should specify all these parameters every time you call this method,
// since they can be overridden in the user interface.
currentFind = rangeOfParams .Find("param1", missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
missing, missing);
while(currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1)
== firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
{
break;
}
currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
currentFind.Font.Bold = true;
currentFind = rangeOfParams.FindNext(currentFind);
}
Replace value of cell:
ws.Cells[2, "A"] = SomeValue;
ws.Cells[2, "E"] = OtherValue;
MSDN Microsoft Excel Solutions
thank you so much!!! i will check the code if understand it. maybe i have a question
– Lin404
Nov 23 '18 at 9:59
Let me know if you have question. This is not the full code, but a path where you can start.
– koviroli
Nov 23 '18 at 10:02
@kovirli can you send me a full code? I mean a working code? I combine this parts with my code so I have some errors like ".. namespace "_Application could not be found (are you missing a using directive or an assembly?
– Lin404
Nov 23 '18 at 12:02
Updated my code, sorre for that, I wrongly pasted that _Application and other names.
– koviroli
Nov 23 '18 at 12:12
add a comment |
The best I can think is:
- Read all lines, like you did in your code
- Parse the lines thatcontains
paramNumto objcets and put these to aDictionary<string, double>. - Iterate through your Keys in Dictionary
and search* for that cell in your excel table associated to your
Key. - If you found the cell that contains the paramter, read it's value
regarding it's location eg.: if the param in
xlWorkSheet.Cells[row,"A"]then in your excel file the value is in
xlWorkSheet.Cells[row-1, "A"]. - Then replace cell value with your dictionary item's value
How to Read xls file:
var excelApp = new Excel.Application();
var xlWorkBook = excelApp.Workbooks.Open(fileName);
var ws = (_Worksheet)wb.Worksheets[1];
Search for cell:
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.Range rangeOfParams = Application.get_Range("A1", "E5");
// You should specify all these parameters every time you call this method,
// since they can be overridden in the user interface.
currentFind = rangeOfParams .Find("param1", missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
missing, missing);
while(currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1)
== firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
{
break;
}
currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
currentFind.Font.Bold = true;
currentFind = rangeOfParams.FindNext(currentFind);
}
Replace value of cell:
ws.Cells[2, "A"] = SomeValue;
ws.Cells[2, "E"] = OtherValue;
MSDN Microsoft Excel Solutions
The best I can think is:
- Read all lines, like you did in your code
- Parse the lines thatcontains
paramNumto objcets and put these to aDictionary<string, double>. - Iterate through your Keys in Dictionary
and search* for that cell in your excel table associated to your
Key. - If you found the cell that contains the paramter, read it's value
regarding it's location eg.: if the param in
xlWorkSheet.Cells[row,"A"]then in your excel file the value is in
xlWorkSheet.Cells[row-1, "A"]. - Then replace cell value with your dictionary item's value
How to Read xls file:
var excelApp = new Excel.Application();
var xlWorkBook = excelApp.Workbooks.Open(fileName);
var ws = (_Worksheet)wb.Worksheets[1];
Search for cell:
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.Range rangeOfParams = Application.get_Range("A1", "E5");
// You should specify all these parameters every time you call this method,
// since they can be overridden in the user interface.
currentFind = rangeOfParams .Find("param1", missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
missing, missing);
while(currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1)
== firstFind.get_Address(Excel.XlReferenceStyle.xlA1))
{
break;
}
currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
currentFind.Font.Bold = true;
currentFind = rangeOfParams.FindNext(currentFind);
}
Replace value of cell:
ws.Cells[2, "A"] = SomeValue;
ws.Cells[2, "E"] = OtherValue;
MSDN Microsoft Excel Solutions
edited Nov 23 '18 at 12:11
answered Nov 23 '18 at 9:38
kovirolikoviroli
683516
683516
thank you so much!!! i will check the code if understand it. maybe i have a question
– Lin404
Nov 23 '18 at 9:59
Let me know if you have question. This is not the full code, but a path where you can start.
– koviroli
Nov 23 '18 at 10:02
@kovirli can you send me a full code? I mean a working code? I combine this parts with my code so I have some errors like ".. namespace "_Application could not be found (are you missing a using directive or an assembly?
– Lin404
Nov 23 '18 at 12:02
Updated my code, sorre for that, I wrongly pasted that _Application and other names.
– koviroli
Nov 23 '18 at 12:12
add a comment |
thank you so much!!! i will check the code if understand it. maybe i have a question
– Lin404
Nov 23 '18 at 9:59
Let me know if you have question. This is not the full code, but a path where you can start.
– koviroli
Nov 23 '18 at 10:02
@kovirli can you send me a full code? I mean a working code? I combine this parts with my code so I have some errors like ".. namespace "_Application could not be found (are you missing a using directive or an assembly?
– Lin404
Nov 23 '18 at 12:02
Updated my code, sorre for that, I wrongly pasted that _Application and other names.
– koviroli
Nov 23 '18 at 12:12
thank you so much!!! i will check the code if understand it. maybe i have a question
– Lin404
Nov 23 '18 at 9:59
thank you so much!!! i will check the code if understand it. maybe i have a question
– Lin404
Nov 23 '18 at 9:59
Let me know if you have question. This is not the full code, but a path where you can start.
– koviroli
Nov 23 '18 at 10:02
Let me know if you have question. This is not the full code, but a path where you can start.
– koviroli
Nov 23 '18 at 10:02
@kovirli can you send me a full code? I mean a working code? I combine this parts with my code so I have some errors like ".. namespace "_Application could not be found (are you missing a using directive or an assembly?
– Lin404
Nov 23 '18 at 12:02
@kovirli can you send me a full code? I mean a working code? I combine this parts with my code so I have some errors like ".. namespace "_Application could not be found (are you missing a using directive or an assembly?
– Lin404
Nov 23 '18 at 12:02
Updated my code, sorre for that, I wrongly pasted that _Application and other names.
– koviroli
Nov 23 '18 at 12:12
Updated my code, sorre for that, I wrongly pasted that _Application and other names.
– koviroli
Nov 23 '18 at 12:12
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.
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%2f53443594%2fhow-to-read-from-excel-and-replace-certain-values-in-a-txt-file%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