Click here to Skip to main content
12,455,917 members (41,845 online)

Read excel cell text along with its format applied

rbitla asked:

Open original thread
Hi,

I have a cell in which has got custom format applied to it and is not autofit.
I want to read content of that cell along with its format.
I have tried cell.value but then it give plain value without format, and if i use cell.text then it gives ### if the cell is not autofit and i dont want to change the width of the column.
Please suggest if there is way to do this, or to format the string using the cell NumberFormat
Please help, any help would be greatly appreaciated.

Below is the code that i have tried.

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();                Microsoft.Office.Interop.Excel.Workbook wbExcel = xlApp.Workbooks.Open("D:\\Test.xlsx");
Microsoft.Office.Interop.Excel.Worksheet wsSheet = (Microsoft.Office.Interop.Excel.Worksheet)wbExcel.Worksheets[1];
Range excelRange = (Range)(wsSheet.Cells[1, 1]);
DateTime excelDate = (DateTime)excelRange.get_Value(Type.Missing);
string strValue = wsSheet.Cells[1, 1].Value2.ToString();
string strText = wsSheet.Cells[1, 1].text;
wbExcel.Close(false, Type.Missing, Type.Missing);
Tags: C#, MS-Excel

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100