Click here to Skip to main content
15,886,519 members
Home / Discussions / C#
   

C#

 
GeneralRe: WPF MVVM Entity Framework(using code First Approach) Pin
Mycroft Holmes4-Jul-19 13:01
professionalMycroft Holmes4-Jul-19 13:01 
AnswerRe: WPF MVVM Entity Framework(using code First Approach) Pin
BillWoodruff4-Jul-19 2:22
professionalBillWoodruff4-Jul-19 2:22 
Questionindex was out of bound of the array Pin
Derbz3-Jul-19 16:50
Derbz3-Jul-19 16:50 
AnswerRe: index was out of bound of the array Pin
phil.o3-Jul-19 17:26
professionalphil.o3-Jul-19 17:26 
GeneralRe: index was out of bound of the array Pin
lmoelleb3-Jul-19 22:36
lmoelleb3-Jul-19 22:36 
AnswerRe: index was out of bound of the array Pin
OriginalGriff3-Jul-19 19:49
mveOriginalGriff3-Jul-19 19:49 
AnswerRe: index was out of bound of the array Pin
bVagadishnu5-Jul-19 7:03
bVagadishnu5-Jul-19 7:03 
QuestionTrying to use VS2017 C# and Excel2007. I need help understanding the error being reported Pin
Greg Gonzales2-Jul-19 19:15
Greg Gonzales2-Jul-19 19:15 
<pre>

C#
using Microsoft.Office.Interop.Excel;
using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelTestConsoleApp1
{
    class Program
    {
        public static string filePath = @"f:\ExcelTest\Tst_Excel.xlsx";

        static void Main(string[] args)
        {
            CreateExcelFile();
            ReadExcelFile();
            Console.ReadLine();
        }

        private static void CreateExcelFile()
        {
            Excel.Application xlApp = new Excel.Application();


            if (xlApp == null)
            {
                Console.WriteLine("Excel is not installed on this system... ");
                return;
            }

            object misValue = System.Reflection.Missing.Value;

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

            xlWorkSheet.Cells[1, 1] = "FirstName";
            xlWorkSheet.Cells[1, 2] = "LastName";
            xlWorkSheet.Cells[2, 1] = "Greg";
            xlWorkSheet.Cells[2, 2] = "Gonzales";

            xlWorkBook.SaveAs(filePath, XlFileFormat.xlExcel12, misValue, misValue, misValue, misValue,
            XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);



            xlWorkBook.Close();
            xlApp.Quit();

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

            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("Excel file created successfully... ");
            Console.BackgroundColor = ConsoleColor.Green;
            
            }

        private static void ReadExcelFile()
        {
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("\nReading the Excel File... ");
            Console.BackgroundColor = ConsoleColor.Green;

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            Excel.Range xlRange = xlWorkSheet.UsedRange;
            int totalRows = xlRange.Rows.Count;
            int totalColumns = xlRange.Columns.Count;

            string firstValue, secondValue;

            for (int rowCount = 1; rowCount <= totalRows; rowCount++)
            {

                firstValue = Convert.ToString((xlRange.Cells[rowCount, 1] as Excel.Range).Text);
                secondValue = Convert.ToString((xlRange.Cells[rowCount, 2] as Excel.Range).Text);

                Console.WriteLine(firstValue + "\t" + secondValue);

            }

            xlWorkBook.Close();
            xlApp.Quit();

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

            Console.WriteLine("End of File... ");
        }

        private static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                Console.WriteLine("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}


xlWorkBook.SaveAs(filePath, XlFileFormat.xlExcel12, misValue, misValue, misValue, misValue,
XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue);

Every time I try to run this code I get this error… 
Exception Unhandled

System.Runtime.InteropServices.COMExceptions:'This extension can not be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type.'
<pre>

AnswerRe: Trying to use VS2017 C# and Excel2007. I need help understanding the error being reported Pin
OriginalGriff2-Jul-19 20:17
mveOriginalGriff2-Jul-19 20:17 
AnswerRe: Trying to use VS2017 C# and Excel2007. I need help understanding the error being reported Pin
Gerry Schmitz2-Jul-19 21:42
mveGerry Schmitz2-Jul-19 21:42 
AnswerRe: Trying to use VS2017 C# and Excel2007. I need help understanding the error being reported Pin
Maciej Los3-Jul-19 8:44
mveMaciej Los3-Jul-19 8:44 
Questionusing c# to copy files from clients to main server Pin
coderz322-Jul-19 12:34
coderz322-Jul-19 12:34 
AnswerRe: using c# to copy files from clients to main server Pin
BillWoodruff2-Jul-19 15:35
professionalBillWoodruff2-Jul-19 15:35 
QuestionHttpException Pin
indra160230-Jun-19 18:50
indra160230-Jun-19 18:50 
AnswerRe: HttpException Pin
Richard Deeming1-Jul-19 0:56
mveRichard Deeming1-Jul-19 0:56 
Questionexception for httpTransportBindingElement Pin
indra160230-Jun-19 17:46
indra160230-Jun-19 17:46 
AnswerRe: exception for httpTransportBindingElement Pin
#realJSOP2-Jul-19 2:46
mve#realJSOP2-Jul-19 2:46 
QuestionHTML to PDF conversion Pin
Member 1164896329-Jun-19 23:59
Member 1164896329-Jun-19 23:59 
AnswerRe: HTML to PDF conversion Pin
OriginalGriff30-Jun-19 1:53
mveOriginalGriff30-Jun-19 1:53 
QuestionFinding many locations of a Bitmap contained inside another Bitmap Pin
IELTS for Beginners29-Jun-19 0:41
IELTS for Beginners29-Jun-19 0:41 
AnswerRe: Finding many locations of a Bitmap contained inside another Bitmap Pin
OriginalGriff29-Jun-19 0:46
mveOriginalGriff29-Jun-19 0:46 
AnswerRe: Finding many locations of a Bitmap contained inside another Bitmap Pin
Richard MacCutchan29-Jun-19 7:24
mveRichard MacCutchan29-Jun-19 7:24 
AnswerRe: Finding many locations of a Bitmap contained inside another Bitmap Pin
BillWoodruff29-Jun-19 19:05
professionalBillWoodruff29-Jun-19 19:05 
QuestionMessage Closed Pin
28-Jun-19 12:37
Member 1451570728-Jun-19 12:37 
SuggestionRe: to Pin
Eddy Vluggen28-Jun-19 13:43
professionalEddy Vluggen28-Jun-19 13:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.