Reputation: 7777
how to read an excel file and change its column data type to string right now i have an excel sheet with column no :10
the data there is been displayed like this
1/5/2010
6/12/2010
i need to change into this format "dd-MM-yyyy" so that it displays like this. 01-05-2010
06-12-2010
can any one please tel me how to do this.
any help would be really great
Upvotes: 0
Views: 2677
Reputation: 1534
it reads and update the excel file.before doing this do the following steps
1.Rightclick solution explorer,then select Add Reference 2.select Microsoft.Office.interop.Excel in .Net Tab 3. Add this line in your code (i.e) in the NameSpace:
using Excel = Microsoft.Office.Interop.Excel;
string address;
Excel.ApplicationClass excel = new Excel.ApplicationClass();
object Missing = Type.Missing;
FileInfo fInfo = new FileInfo(@"D:\Book1.xls");
if (fInfo.Exists)
{
Excel.Workbook workbook = excel.Workbooks.Open(@"D:\Book1.xls", Missing, Missing,
Missing, Missing, Missing, Missing, Missing,
Missing, Missing, Missing, Missing, Missing,
Missing, Missing);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets["Sheet1"];
Excel.Range docNumber = worksheet.Cells.Find("10/5/2010", worksheet.Cells[1, 1],
Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlPart, Missing, Excel.XlSearchDirection.xlNext,
false, Missing, Missing);
if (docNumber != null)
{
address = docNumber.get_Address(true, true, Excel.XlReferenceStyle.xlA1, Missing,
Missing);
docNumber = worksheet.UsedRange;
DateTime parsedDate;
for (int rCnt = 1; rCnt <= docNumber.Rows.Count; rCnt++)
{
for (int cCnt = 1; cCnt <= docNumber.Columns.Count; cCnt++)
{
string str = (string)(docNumber.Cells[rCnt, cCnt] as Excel.Range).Text ;
parsedDate = DateTime.Parse(str);
str = String.Format("{0:dd-mm-yyyy}", parsedDate);
docNumber.Cells[rCnt, cCnt] = str;
}
}
workbook.Save();
}
}
Upvotes: 2