Tuesday, 09 September 2014 02:17

Convertors

Written by  Administrator
Rate this item
(1 Vote)
This demo shows you how to convert an Excel document (xls/xlsx) to PDF, HTML, Image file format.
{k2-demo src:$server/Spire.XLS/21_Misc/Convertors.aspx|style:height:189px}
using System;
using Spire.Xls;
using System.Drawing;
using System.Drawing.Imaging;

namespace DemoOnlineCode
{
    class Convertors
    {
        public void demoConvert(String filePath, string format,string resultFileName)
        {
            Workbook book = new Workbook();
            book.LoadFromFile(filePath)  ;
            ConvertFormat(book, format, resultFileName);
        }
        private void ConvertFormat(Workbook workbook, string format, string resultFileName)
        {
            switch (format)
            {
                case "PDF":
                    workbook.SaveToFile(resultFileName + ".pdf", Spire.Xls.FileFormat.PDF);
                    break;
                case "Image":
                    Image[] images = new Image[workbook.Worksheets.Count];
                    for (int i = 0; i < workbook.Worksheets.Count; i++)
                    {
                        images[i] = workbook.SaveAsImage(i, 96, 96);
                    }
                    if (images != null && images.Length > 0)
                    {
                        if (images.Length == 1)
                        {
                            images[0].Save(resultFileName+".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                        }
                        else
                        {
                            for (int i = 0; i < images.Length; i++)
                            {
                                String fileName = String.Format("{0}-Image-{1}.png", resultFileName, i);
                                images[i].Save(fileName, ImageFormat.Png);
                            }
                        }
                    }
                    break;

                case "HTML":   
                    for (int i = 0; i < workbook.Worksheets.Count; i++)
                    {
                        Worksheet sheet = workbook.Worksheets[i];
                        string htmlPath = string.Format(resultFileName+"-{0}.html", i++);
                        sheet.SaveToHtml(htmlPath);
                    }                  
                    break;
            }
        }
    }
}


Imports Spire.XLS
Imports System.Drawing
Imports System.Drawing.Imaging

Namespace DemoOnlineCode
    Class Convertors
        Public Sub demoConvert(filePath As [String], format As String, resultFileName As String)
            Dim book As New Workbook()
            book.LoadFromFile(filePath)
            ConvertFormat(book, format, resultFileName)
        End Sub
        Private Sub ConvertFormat(workbook As Workbook, format As String, resultFileName As String)
            Select Case format
                Case "PDF"
                    workbook.SaveToPdf(resultFileName & ".pdf", Spire.Xls.FileFormat.PDF)
                    Exit Select

                Case "Image"
                    Dim images As Image() = New Image(workbook.Worksheets.Count - 1) {}
                    For i As Integer = 0 To workbook.Worksheets.Count - 1
                        images(i) = workbook.SaveAsImage(i, 96, 96)
                    Next
                    If images IsNot Nothing AndAlso images.Length > 0 Then
                        If images.Length = 1 Then
                            images(0).Save(resultFileName & ".bmp", System.Drawing.Imaging.ImageFormat.Bmp)
                        Else
                            For i As Integer = 0 To images.Length - 1
                                Dim fileName As [String] = [String].Format("{0}-Image-{1}.png", resultFileName, i)
                                images(i).Save(fileName, ImageFormat.Png)
                            Next
                        End If
                    End If
                    Exit Select

                Case "HTML"
                    For i As Integer = 0 To workbook.Worksheets.Count - 1
                        Dim sheet As Worksheet = workbook.Worksheets(i)
                        Dim htmlPath As String = String.Format(resultFileName & "-{0}.html", System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1))
                        sheet.SaveToHtml(htmlPath)
                    Next
                    Exit Select
            End Select
        End Sub
    End Class
End Namespace



Additional Info

  • title:
  • name:
  • path:
Last modified on Thursday, 29 January 2015 09:41
More in this category: Charts »