Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

As I need to read Excel Macro and save into text file in C# and we don't use COM object ( Interop ) .

Any suggestion is Must appericate ?

What I have tried:

I have tried via Nuget package ( NPOI ) but not able to read Macro .


Below is the Sample code to read file .

C#
using (var fs = new FileStream("a.xlsx", FileMode.Open, FileAccess.Read))
{
	wb = new XSSFWorkbook(fs);

	for (int i = 0; i < wb.Count; i++)
	{
		comboBox1.Items.Add(wb.GetSheetAt(i).SheetName);
	}


	}

	// clear grid before filling
	dataGridView1.Rows.Clear();
	dataGridView1.Columns.Clear();

	// get sheet
	sh = (XSSFSheet)wb.GetSheet(comboBox1.SelectedItem.ToString());




	int i = 0;
	while (sh.GetRow(i) != null)
	{
		// add necessary columns
		if (dataGridView1.Columns.Count < sh.GetRow(i).Cells.Count)
		{
			for (int j = 0; j < sh.GetRow(i).Cells.Count; j++)
			{
				dataGridView1.Columns.Add("", "");
			}
		}

		// add row
		dataGridView1.Rows.Add();

		// write row value
		for (int j = 0; j < sh.GetRow(i).Cells.Count; j++)
		{
			var cell = sh.GetRow(i).GetCell(j);

			if (cell != null)
			{
				// TODO: you can add more cell types capability, e. g. formula
				switch (cell.CellType)
				{
					case NPOI.SS.UserModel.CellType.Numeric:
						dataGridView1[j, i].Value = sh.GetRow(i).GetCell(j).NumericCellValue;
						break;
					case NPOI.SS.UserModel.CellType.String:
						dataGridView1[j, i].Value = sh.GetRow(i).GetCell(j).StringCellValue;
						break;
				}
			}
		}

		i++;
	}
}
Posted
Updated 20-Oct-20 3:12am
v3
Comments
CHill60 20-Oct-20 6:27am    
What have you tried. Share your code
CHill60 20-Oct-20 9:06am    
You can use the "Improve Question" link to add details to your question. I have done that for you this time.
So you have some code, and you have told us what you want but you haven't described your problem - "no luck" does not help us at all
subodhan 20-Oct-20 9:13am    
Thanks I have Improve the question .
Requesting to assist .
[no name] 20-Oct-20 14:55pm    
Seems like a pointless exercise, since you can't run the "text file" macro. Save the Work Book if you need a "backup" (since your next exercise would probably be how to get the macro back into Excel).
subodhan 22-Oct-20 3:44am    
Hi Gerry ,
I need to copy Excel Macro in Text file to compare macro has change or not also no need to run the Macro.

I need to simple copy all macro in text file .( without Interop object )

Hope you can help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900