This application takes a Microsoft Excel file as input, reads it, and writes its content to a .NET resource file. The code uses a COM component named Microsoft Excel 11.0 Object Library; it provides APIs to manipulate the Excel application, so before using this code, you need to add the reference to this DLL as shown in Fig. 1 above. If a Workbook contains more than one worksheet, it will read all the worksheets and will generate different resource files for different Worksheets. Keep in mind the Excel file should be in the same format as the .NET recourse files are, like KEY and VALUE columns only (see Fig. 2).
There is a functionality called Localization in .NET which most web application developers in ASP.NET must be familiar with. I came across implementing the same functionality in one of my projects, and for that, I needed to create a resource file for different languages. I made a resource file for English language, but I also needed the values in different languages for the same keys, so I put the key value pairs in an Excel file and gave it to the designated person who was going to write the corresponding values for other languages. I got the resultant Excel file but it was very hectic to produce resource files from that manually, and then an idea struck my mind, and I developed this tool that did the task for me very efficiently. See the Resource to Excel file conversion article also.
Using the Code
This is the Excel application initialization code you can put in the page load of the form:
ExlObj = new Excel.Application();
if (ExlObj == null)
MessageBox.Show("Problem in starting Excel.","Error",
ExlObj.Visible = true;
This is the code that performs the actual task:
private void btnStart_Click(object sender, EventArgs e)
System.Resources.ResXResourceWriter resWriter = null;
if (txtFilePath.Text.Trim() != "")
Excel.Workbook theWorkBook =
ExlObj.Workbooks.Open(txtFilePath.Text, 0, true, 5,"",
"", true, Excel.XlPlatform.xlWindows,
"\t", false, false, 0, true, null, null);
Excel.Sheets sheets = theWorkBook.Worksheets;
for (int index = 1; index <= sheets.Count; index++)
Excel.Worksheet workSheet = (Excel.Worksheet)sheets.get_Item(index);
resWriter = new System.Resources.ResXResourceWriter(
workSheet.Name.ToString() + ".resx");
int noOfDataRowsInExcelFile = workSheet.get_Range("A65536",
for (int rowIndex = 1; rowIndex <= noOfDataRowsInExcelFile; rowIndex++)
Excel.Range range = workSheet.get_Range("A" +
rowIndex.ToString(), "B" + rowIndex.ToString());
System.Array itemArray = (System.Array)range.Cells.Value2;
string key = itemArray.GetValue(1, 1) == null ?
string value = itemArray.GetValue(1, 2) == null ?
if (key.Trim() != "")
resWriter = null;
MessageBox.Show("Operation completed successfully.");
MessageBox.Show("Input the excel file.");
catch (Exception ex)
Points of Interest
The basic idea behind this code is to read the Excel file. The sample I presented here just fits my requirements, but you can do a lot with this with a little exploration. With a little modification to the code, you can export the Excel data to any programming object like a DataSet, Table, DataGrid etc.