|
/* This programme reads the input excel file and then writes its contents to a .Net resource file
* If a workbook contains more than one worksheets 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 resourse files are, like KEY and VALUE columns only*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ExcelToResourceConversion
{
public partial class Form1 : Form
{
// creating an excel object reference
Excel.Application ExlObj = null;
string fileName = string.Empty;
public Form1()
{
InitializeComponent();
// Starting an excel application, as it is necessary to manipulate the excel file
ExlObj = new Excel.Application();
// Showing the error message if any problem occures in starting excel application
if (ExlObj == null)
{
MessageBox.Show("Problem in starting Excel.","Error",MessageBoxButtons.OK ,MessageBoxIcon.Error);
System.Windows.Forms.Application.Exit();
}
// you can keep the excel application visible or invisible while writing to it
ExlObj.Visible = true;
}
private void btnStart_Click(object sender, EventArgs e)
{
// Creating a reference to .Net resource file object
System.Resources.ResXResourceWriter resWriter = null;
try
{
if (txtFilePath.Text.Trim() != "")
{
// Opening the excel file. in excel version 5.0.0 it takes 15 arguments some of which like filepath, format, origin etc are mandatory, other you can put null or 0.
Excel.Workbook theWorkBook = ExlObj.Workbooks.Open(txtFilePath.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, null, null);
// Extracting the worksheet collection out of workbook, there can be any number of worksheet in a workbook
Excel.Sheets sheets = theWorkBook.Worksheets;
progressBar1.Maximum = sheets.Count;
// Iterating for each worksheet
for (int index = 1; index <= sheets.Count; index++)
{
progressBar1.Value = index;
// Extracting a single worksheet out of the collection of worksheet
Excel.Worksheet workSheet = (Excel.Worksheet)sheets.get_Item(index);
// Initializing the resource writer object
resWriter = new System.Resources.ResXResourceWriter(workSheet.Name.ToString() + ".resx");
// Calculating the total number of data rows in a worksheet
// here "A65536" is the index of last row in excel sheet
// get_End method gives the index of data row after which it finds an empty row
// so that's why we start it from last most index of sheet.
int noOfDataRowsInExcelFile = workSheet.get_Range("A65536", "A65536").get_End(Excel.XlDirection.xlUp).Row;
// Iterating for number of data rows in a worksheet
for (int rowIndex = 1; rowIndex <= noOfDataRowsInExcelFile; rowIndex++)
{
// get_range gives the object of a particular row from index1 to index2
Excel.Range range = workSheet.get_Range("A" + rowIndex.ToString(), "B" + rowIndex.ToString());
// Cells.Value2 returns the array of alements in that row
System.Array itemArray = (System.Array)range.Cells.Value2;
// Extracting the values from array
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() != "")
{
// Writing the values to .Net resource file
resWriter.AddResource(key, value);
}
}
// Colosing the Resource Writer. It is a necessary step to make the changes permanent to a
// resorce file otherwise you could also call Generate method in order to commit changes to
// file but Close does both tasks closing as well as commit.
resWriter.Close();
resWriter = null;
}
MessageBox.Show("Operation completed successfully.");
progressBar1.Value = 0;
}
else
{
MessageBox.Show("Input the excel file.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
ExlObj.Application.Quit();
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
this.openFileDialog1.FileName = "*.xls";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFilePath.Text = openFileDialog1.FileName;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.