C # Document Conversion-excel Pdf/image/html/txt/xml/xps/csv/ods, etc.

Source: Internet
Author: User


You may encounter a situation in which a file is converted to another format in your daily work, and in this article you will learn how to use the control free Spire.xls for. NET (Community Edition) to convert Excel documents to PDF, IMAGE, HTML, TXT, XML, XPS, CSV, ODS, SVG, EMF, xlsm and other formats for your reference.
Tool Use:After installation, reference Spire.XLS.dll in the project (the DLL can be obtained in the Bin folder under the installation file)





Here is a detailed description:


1. Excel to PDF


(This component also supports converting charts, shapes, SmartArt graphics, and images in Excel documents to PDF)


1.1 Retweets entire Excel workbook for PDF

// Create a Wordbook class object and load the Excel document to be converted
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ test.xlsx", ExcelVersion.Version2010);
// Save the Excel document as PDF and open the converted PDF document
workbook.SaveToFile ("result.pdf", Spire.Xls.FileFormat.PDF);
System.Diagnostics.Process.Start ("result.pdf");
Conversion result:

1.2 Convert the designated worksheet to PDF
// Create workbook and load Excel document
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Sample.xlsx");
// Get the first worksheet
Worksheet sheet = workbook.Worksheets [0];
// Save as PDF and open the document
sheet.SaveToPdf ("toPDF.pdf");
System.Diagnostics.Process.Start ("toPDF.pdf");
Conversion result:

2. Excel to Image 2.1 Convert the specified worksheet to Image
// Initialize a Workbook instance and load a workbook file
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Sample.xlsx");

// Save the first worksheet as a picture
Worksheet sheet = workbook.Worksheets [0];
sheet.SaveToImage ("sample.jpg");
2.2 Convert the cells in the specified range of the worksheet to Image
// Create an instance of the Workbook class and load a worksheet from the file
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ test.xlsx");

// Get the first worksheet in the document
Worksheet sheet = workbook.Worksheets [0];

// Specify the range of cells, save as the desired picture format
sheet.SaveToImage (3, 1, 4, 3) .Save ("image1.png", ImageFormat.Png);
sheet.SaveToImage (5, 1, 19,3) .Save ("image2.jpeg", ImageFormat.Jpeg);
sheet.SaveToImage (20, 1, 21, 3) .Save ("image3.bmp", ImageFormat.Bmp);
sheet.SaveToImage (22, 1, 23, 3) .Save ("image4.bmp", ImageFormat.Bmp);
3. Excel to HTML
// Create a workbook class object and load the Excel document
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ sample.xlsx");

// Get the first worksheet
Worksheet sheet = workbook.Worksheets [0];
// Save as Html file and open the document
sheet.SaveToHtml ("sample.html");
System.Diagnostics.Process.Start ("sample.html");
4. Excel to TXT
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ sample1.xlsx");
Worksheet sheet = workbook.Worksheets [0];
sheet.SaveToFile ("ExceltoTxt.txt", "", Encoding.UTF8);
5. Excel to XML
Workbook wb = new Workbook ();
wb.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ sample1.xlsx");
wb.SaveAsXml ("result.xml");
6. Excel to XPS
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ sample.xlsx", ExcelVersion.Version2010);
workbook.SaveToFile ("result.xps", Spire.Xls.FileFormat.XPS);
7. Excel to CSV
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ test.xlsx");
Worksheet sheet = workbook.Worksheets [0];
sheet.SaveToFile ("sample.csv", "", Encoding.UTF8);
8. Excel to ODS
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ test.xlsx");
workbook.SaveToFile ("Result.ods", FileFormat.ODS);
9. Excel to SVG
            // Create a Wordkbook class object and load the Excel document
            Workbook workbook = new Workbook ();
            workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ Sample.xlsx");
// Traverse the Excel workbook, call the method ToSVGStream () to save the file to the stream, and create an SVG file from the stream
            for (int i = 0; i <workbook.Worksheets.Count; i ++)
            {
                FileStream fs = new FileStream (string.Format ("E: \\ Program Files \\ sheet- {0} .svg", i), FileMode.Create);
                workbook.Worksheets [i] .ToSVGStream (fs, 0, 0, 0, 0);
                fs.Flush ();
                fs.Close ();
         }
10. Excel to EMF
// Initialize Workbbok class instance and load Excel document
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ test.xlsx");
// Get the first worksheet
Worksheet sheet = workbook.Worksheets [0];
// Save the worksheet in the specified size range in Emf format
sheet.SaveToEMFImage ("result.emf", 1, 1, 19, 6, System.Drawing.Imaging.EmfType.EmfPlusDual);
11. Excel to XLSM
Workbook workbook = new Workbook ();
workbook.LoadFromFile (@ "C: \ Users \ Administrator \ Desktop \ test.xlsx", ExcelVersion.Version97to2003);
workbook.SaveToFile ("result.xlsm", FileFormat.Version2007);
All of the above is the introduction of the method of converting Excel to PDF, IMAGE, HTML, TXT, XML, XPS, CSV, ODS, SVG, EMF, XLSM and other format files. If you like it, please reprint (please indicate the source) .
Thanks for browsing!

C # document conversion—Excel to PDF / IMAGE / HTML / TXT / XML / XPS / CSV / ODS etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.