Rss Feed Like Us on facebook Google Plus
Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

June 4, 2013

Generate Excel to Export Data using Datatable

Generate Excel is a Major Feature which is used frequently in Web Applications as well as
in Console Applications. So i am going to present an article about generating excel with MS-Excel installed on and without MS-Excel installed on target computer.

Problem with Excel Automation
The technique that is most frequently used to transfer data to an Excel workbooks is Automation. With Automation, you can call methods and properties that are specific to Excel tasks, but this solution has many drawbacks. Some of them are described in the Microsoft Knowledge Base. Additionally, you have to manage the lifetime of the temporary Excel files(.xls) created on the server. Also, it is slow, because Excel runs in a separate process.


  • You can generate excel without MS-Office installed on target computer..
  • You Can Export Data Table data to created Excel

class GenerateExcel
 
{
//  This function will Genereate Excel without MS-Office installed on target computer
 public void CreateExcel(string fileName)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
                Console.WriteLine("** Excel Deleted ***");
            }
            if (!File.Exists(fileName))
            {
                FileStream stream = new FileStream(filename, FileMode.OpenOrCreate); 
                stream.Close();
                Console.WriteLine("** Excel Created ***");
            }
        }
  
        public void createExcelwithMSOffice(string fileName)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
                Console.WriteLine("** Excel Deleted ***");
                swlog.WriteLine("***  Excel Exists....Excel Deleted.....  ***");
            }
            if (!File.Exists(fileName))
            {
                Application xl = null;
                _Workbook wb = null;
                object misValue = System.Reflection.Missing.Value;
 
                xl = new Application();
                xl.SheetsInNewWorkbook = 1;
                wb = (_Workbook)(xl.Workbooks.Add(Missing.Value));
                wb.SaveAs(fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                wb.Close(true, misValue, misValue);
                Console.WriteLine("** Excel Created ***");
                swlog.WriteLine("***    New Excel Created     ***");
            }
        }
 
 //  This function will export Datatable Data to Created Excel......

        public void ExportToSpreadsheet(System.Data.DataTable table, string name)
        {
            StreamWriter sw = null;
            sw = new StreamWriter(name);
            string ColValue;
            string ColName = "";
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    ColName = table.Columns[i].ColumnName.ToString() + "\t";
                    sw.Write(ColName);
                }
                sw.WriteLine("\t");
                foreach (DataRow row in table.Rows)
                {
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        ColName = table.Columns[i].ColumnName.ToString();
                        ColValue = row[i].ToString();
                        ColValue = ColValue.ToString().Replace(",", string.Empty) + "\t";
                        ColValue = ColValue.ToString().Replace(Environment.NewLine, " ");
                        ColValue = ColValue.ToString().Replace("\n", " ");
                        //ColValue = ColValue.ToString().Replace(" ", "");
                        ColValue = ColValue.ToString().Replace("-Select-;", "");
                        if (table.Columns[i].DataType.Name == "Boolean")
                        {
                            ColValue = ColValue.ToString().Replace("True", "Yes");
                            ColValue = ColValue.ToString().Replace("False", "No");
                        }
                        sw.Write(ColValue);
                    }
                    sw.WriteLine("\t");
                }
                sw.Close();
                Console.WriteLine("** Data Exported to Excel ***");
            }
            else
            {
                Console.WriteLine("** No Data to Export in Excel ***");
                Environment.Exit(0);
            }
        }
}

Calling the functions..


class Program
    {
        static void Main(string[] args)
        {
           System.Data.DataTable dt = new DataTable();
           GenerateExcel oexcel = new GenerateExcel ();
           string filename=@"C:\demo.xls";
           oexcel.CreateExcel(filename);
           oexcel.ExportToSpreadsheet(dt,filename);
         }
    } 
I hope this article gives you a head start in working with Excel files from .NET and C#.
Read More

May 30, 2013

Aged programmers are better than youth :survey

A recent study from North Carolina State University has finally revealed an unconventional truth for a youth obsessed industry- Computer programmers actually gets better with age.


The results from this study will surely spread light into the dark corners of tech companies, especially start-ups, who had a general notion of favoring the youth for employment.

“The older developers seemed to know more than the younger developers did,” says Emerson Murphy-Hill, co-author of the study. “That runs contrary to what the popular expectation is.”


“Particularly traits like vocabulary, knowledge of history and life experience — skills key to programming — improve with time. But programming knowledge, too, can be maintained at a high level into a person’s 50s and 60s,” he continued.

However the present conditions at start-ups and tech firms seem hard to change. Tech hiring is not just about the skills and experience now, these companies always look for a breed of programmers, who are free from responsibilities, family and don’t have high salary expectations. Unfortunately, the seasoned programmers don’t fit into any of those categories.
Read More

February 23, 2012

Top 10 Most Popular Programming Language

The knowledge of a handful of programming languages could come to be a lifesaver to many a programmer, especially since most languages that were popular 10 years ago are not as viable as they are now.

But there are many developers who have earned their worth simply by knowing the right programming language at the right time, simply because they had solid skills that were profitable while the language was popular.

Here are some languages though, which stayed popular through the years, and prove to give young developers a jumpstart to their careers, and always are a bonus to add to any developer’s resume, as compiled by TIOBE software, a coding standards company.

1. Java

What is it?
 An object-oriented programming language developed in the late 1990s by James Gosling and colleagues at Sun Microsystems.

Why is it important?
This “beautiful” programming language is central for any non-Microsoft developer, i.e. any developer who focuses on the non-.NET experience. It is mostly derived from C and C++ but has a more basic object model. It ranked first on TIOBE’s list of most popular programming languages.

2. C

What is it?
C, a general purpose programming language built by Dennis Ritchie when he was a part of Bell Telephone labs, is the bass of C++ and other programming languages. It was built to work with the Unix operating system.

Why is it important?
C is one of the most widely used programming languages of all time, and ranked second on the list. “Learning C is crucial. Once you learn C, making the jump to Java or C# is fairly easy, because a lot of the syntax is common. Also, a lot of C syntax is used in scripting languages,” Wayne Duqaine, director of Software Development at Grandview Systems, of Sebastopol, Calif., told eWEEK.
 
3. C#

What is it?
This general-purpose programming language developed by Microsoft evolved from C and C++ as a part of the software company’s .NET initiative.

Why is it important?
This language is an essential part of the .NET framework, so developers who use Microsoft heavily will find it critical, according to Duqaine.

4. C++

What is it?
C++ is a general purpose multi-paradigm spanning compiled language that has both high-level and low-level languages’ features. It was started as an enhancement to the C programming language, Bjarne Stroustrup in 1979.

Why is it important?
It is one of the most popular programming languages, winning fourth place on the list, with application domains including systems software, application software, server and client applications, and entertainment software such as video games.  The language has also greatly influenced many other popular programming languages, such as C# and Java.

5. Objective-C
 
What is it?
This object-oriented programming language created first by Brad Cox and Tom Love at their company Stepstone in the early 1980s, adds Smalltalk-like messaging to the C programming language.

Why is it important?
This language is most used on the Apple iOS and Mac OS X. Objective-C is the principal language used for Apple's Cocoa API as well.

6. PHP

What is it?
This language is especially suited for Web development because of it easy embedding into HTML pages. It is an open-source, server-side, cross-platform, interpretive HTML scripting language

Why is it important?
It is a popular language, ranking sixth on TIOBE’s list. "High-speed scripting with caching, augmented with compiled code plug-ins (such as can be done with Perl and PHP) is where the future is. Building Web apps from scratch using C or COBOL is going the way of the dinosaur," said Duquaine, according to eWEEK’s report.

7. (Visual) Basic

What is it?
This is an event-driven programming language which is implemented on Microsoft’s .Net framework.

Why is it important?
This language ranked as the seventh most popular language on TIOBE’s list, probably because it was designed by Microsoft to be easy to learn and use. According to Tim Huckaby, CEO of San Diego-based software engineering company CEO Interknowlogy.com, “It is currently dominating in adoption and that is where all the work is,” as in eWEEK’s report.

 8. Python

What is it?

This is an event-driven programming language which is extensively used by Google because of its simplicity. It is managed by the Python Software Foundation.

Why is it important?

Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to combine "remarkable power with very clear syntax", and its standard library is large and comprehensive.

It is releases on 4 September 2011, 6 months ago.It is developed by Python Software Foundation.

9. Perl

What is it?
Being a high-level programming language, its emphasis lies in code readability and clear syntax. It combines Object-oriented and functional programming styles, and is often used as a scripting language. Perl is an open-source language used widely to process text through CGI programs.


Why is it important?
Perl’s efficiency in processing of piles of text has ranked it ninth in terms of programming language popularity. It is used extensively to write Web server programs for a variety of tasks. “Learning some form of scripting language, such as Perl or PHP is critical if you are doing Web apps," told Wayne Duqaine, director of Software Development at Grandview Systems, of Sebastopol, Calif., in a talk with eWEEK.

10. JavaScript

What is it?
JavaScript is an object-oriented scripting language that is smaller than Java. Being a client-side language, it runs in the web browser on the client-side with a simplified set of commands, easier code and no need for compilation.

Why is it important?

JavaScript is simple to learn and is the tenth most widely used programming language. It is used in millions of web pages to authenticate forms, detect browsers and improve design, and it is easier to run these functions as it is embedded into HTML.
Read More

© 2011-2016 Techimpulsion All Rights Reserved.


The content is copyrighted to Tech Impulsion and may not be reproduced on other websites.