We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Monday, July 11, 2016

How to convert a file one format to another format in C#

Hi friends! I am explaining how to convert a file one format to another format in c#.Net. We have seen when we convert a file one format to other format files then we have to write a number of lines of codes in C# but here I am not writing many more lines of code in the program. Here I am writing fewer numbers of lines of code. Here I am using a technique that is called Spire.Office. This is very easy to use for programmers.

Now first we have to know, what is Spire.Office?

Spire.Office for .NET is a combination of Enterprise-Level Office .NET components offered by E-iceblue. It includes Spire.Doc, Spire XLS, Spire.Spreadsheet, Spire.Presentation, Spire.PDF, Spire.DataExport, Spire.PDFViewer, Spire.DocViewer and Spire.BarCode. Spire.Office contains the most up-to-date versions of the above .NET components.

With Spire.Office for .NET, developers can create a wide range of applications. It enables developers to open, create, modify, convert, print, View MS Word, Excel, PowerPoint and PDF documents. Furthermore, it allows users to export data to popular files such as MS Word/Excel/RTF/Access, PowerPoint, PDF, XPS, HTML, XML, Text, CSV, DBF, Clipboard, SYLK, etc.

For more details, go to this link .
How to use this? And how to implement in c# program?
For this follow some steps:


Step 1:

  • We have to download Spire.office.To download this we can go this link.
  • After download, Extract that file and install the setup file.
When we install the setup file then automatically generates dll files for all formats in which we have to convert ex. Doc,Pdf,XML etc in C:\Program Files\e-iceblue\Spire.Office\Bin\NET4.0 location.


Step 2:
Now we have to implement these dll files according to requirement.

  • For this, first, we have to add dll files which we want to convert files from one format to another format.
  • Right-click of solution explorer then go to “Add” option and then go to the “Reference” option after this select “Browse” option and go to “C:\Program Files\e-iceblue\Spire.Office\Bin\NET4.0”  location and choose DLL file and click “Add” button.
Step 3:
Now we write code
Doc file to pdf file
Add namespace
using Spire.Doc;
2.       Write CSharp code
class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"C:\Users\mithilesh kumar\Desktop\Calanders.docx");
            document.SaveToFile(@"C:\Users\mithilesh kumar\Desktop\WebApi1.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start(@"C:\Users\mithilesh kumar\Desktop\WebApi1.pdf");
            Console.ReadLine();
        }
    }
}

Output:

Text file to Doc file
  class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"C:\Users\mithilesh kumar\Desktop\text.txt");
            document.SaveToFile(@"C:\Users\mithilesh kumar\Desktop\Result.doc", FileFormat.Doc);
            System.Diagnostics.Process.Start(@"C:\Users\mithilesh kumar\Desktop\Result.Doc");
            Console.ReadLine();
        }
    }

Output:

Edit Excel file

using Spire.Xls;

namespace SpireExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\Users\mithilesh kumar\Desktop\Demo.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Range["A1"].Text = "Sr No";
            sheet.Range["A1"].Style.Font.FontName = "Arial Narrow";      
            sheet.Range["A1"].Style.Font.Color = System.Drawing.Color.DarkBlue;       
            sheet.Range["B1"].Value = "Name";      
            sheet.Range["B1"].Style.Font.FontName = "Book Antiqua";        
            sheet.Range["B1"].Style.Font.Color =System.Drawing.Color.DarkOrange;
            sheet.Range["C1"].Value = "Address";
            sheet.Range["C1"].Style.Font.FontName = "Book Antiqua";
            sheet.Range["C1"].Style.Font.Color = System.Drawing.Color.Gray;
            sheet.Range["D1"].Value = "Email";
            sheet.Range["D1"].Style.Font.FontName = "Book Antiqua";
            sheet.Range["D1"].Style.Font.Color = System.Drawing.Color.Green;
            sheet.Range["E1"].Value = "Email";
            sheet.Range["E1"].Style.Font.FontName = "Book Antiqua";
            sheet.Range["E1"].Style.Font.Color = System.Drawing.Color.Gray;
            workbook.SaveToFile(@"C: \Users\mithilesh kumar\Desktop\EditSheet.xlsx", ExcelVersion.Version2007);  
            System.Diagnostics.Process.Start(@"C: \Users\mithilesh kumar\Desktop\EditSheet.xlsx");

        }
    }
}
Output:
Create ppt

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace SpireExample
{
    class PPT
    {
        static void Main()
        {
            //create PPT document

            Presentation presentation = new Presentation();
            //add new shape to PPT document         

            IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle,
                                new RectangleF(0, 50, 200, 50));

            shape.ShapeStyle.LineColor.Color = Color.White;
            shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.None;
            //add text to shape
            shape.AppendTextFrame("Hello, I am mithilesh,Welcome to my ppt");
            //set the Font fill style of text
            TextRange textRange = shape.TextFrame.TextRange;
            textRange.Fill.FillType = FillFormatType.Solid;
            textRange.Fill.SolidColor.Color = Color.Green;
            textRange.LatinFont = new TextFont("Verdana");
            //save the document
            presentation.SaveToFile(@"C:\Users\mithilesh kumar\Desktop\Mks.ppt", FileFormat.Ppsx2007);
            System.Diagnostics.Process.Start(@"C:\Users\mithilesh kumar\Desktop\Mks.ppt");

        }
    }
}
 Output

Conclusion:-

This is a really great library. We can convert a file one format to another format in an easy way. So I hope this is easily understandable. In the next part, I will explain how we can use this library in asp.Net.

No comments: