Fig. 1
Fig. 2
Introduction
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).
Background
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",
MessageBoxButtons.OK ,MessageBoxIcon.Error);
System.Windows.Forms.Application.Exit();
}
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;
try
{
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",
"A65536").get_End(Excel.XlDirection.xlUp).Row;
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.Empty :
itemArray.GetValue(1, 1).ToString();
string value = itemArray.GetValue(1, 2) == null ?
string.Empty :
itemArray.GetValue(1, 2).ToString();
if (key.Trim() != "")
{
resWriter.AddResource(key, value);
}
}
resWriter.Close();
resWriter = null;
}
MessageBox.Show("Operation completed successfully.");
}
else
{
MessageBox.Show("Input the excel file.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
ExlObj.Application.Quit();
}
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.
History