Click here to Skip to main content
15,998,056 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to export the data from c# code to excel sheet and plot the graph.I have two values one is temperature and other type is DateTime.I m exporting the data successfully to excel and plotting the graph also.But with Date on x-axis i am getting time as 00:00.
In excel sheet only to get the time in correct format.I went to settings.

1. Right click at the X axis in the chart, and select Format Axis from the context menu.
2. Then in the Format Axis pane or Format Axis dialog, under Axis Options tab, check
Text axis option in the Axis Type section.
In Axis type
a.Automatically select text axix
b.Text Axis
c.Date Axis
I selected the text axis .Then I got the date time on the X axis.

My question is,How can i achieve in c# code.
thank you in advance.

What I have tried:

private void button1_Click(object sender, EventArgs e)
       {
           Excel.Application xlApp;
           Excel.Workbook xlWorkBook;
           Excel.Worksheet xlWorkSheet;
           object misValue = System.Reflection.Missing.Value;

           xlApp = new Excel.Application();
           xlWorkBook = xlApp.Workbooks.Add(misValue);
           xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

           //add data

           xlWorkSheet.Cells[1, 1] = "";
           xlWorkSheet.Cells[1, 2] = "Temperature";

           for (int i = 0; i < mytemp.Length; i++)
           {

               xlWorkSheet.Cells[i + 2, 1] = datetimeArray[i];
               xlWorkSheet.Cells[i + 2, 2] = mytemp[i];
           }
           string number = 'd' + mytemp.Length.ToString();
           Excel.Range chartRange;

           Excel.ChartObjects xlCharts =
          (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
           Excel.ChartObject myChart =
          (Excel.ChartObject)xlCharts.Add(200,200,200,200);
           Excel.Chart chartPage = myChart.Chart;

           Excel.Axis xAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue,
           Excel.XlAxisGroup.xlPrimary);
           xAxis.HasTitle = true;
           xAxis.AxisTitle.Caption = "Date Time";




           chartRange = xlWorkSheet.get_Range("A1",number);
           chartPage.SetSourceData(chartRange, misValue);
           chartPage.ChartType = Excel.XlChartType.xlLine;

           //export chart as picture file
           chartPage.Export(@"C:\Users\Akhand Jyoti\Desktop\Receive2", "BMP",
           misValue);

           xlWorkBook.SaveAs("graph.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
           xlWorkBook.Close(true, misValue, misValue);
           xlApp.Quit();

           releaseObject(xlWorkSheet);
           releaseObject(xlWorkBook);
           releaseObject(xlApp);

           MessageBox.Show("Excel file created , you can find the file c:\\csharp-
           Excel.xls");


   }
   private void releaseObject(object obj)
  {
           try
           {
               System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
               obj = null;
           }
           catch (Exception ex)
           {
               obj = null;
               MessageBox.Show("Exception Occured while releasing object " +
               ex.ToString());
           }
           finally
           {
               GC.Collect();
           }
    }
Posted
Comments
Maciej Los 7-Feb-18 16:38pm    
Does Excel display date data type properly?
Member 13488719 7-Feb-18 23:20pm    
yes it is displaying.After formatting the axes and set the axis type to =Text Axis.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900