C# Excel转PDF、image、HTML、TXT、XML、XPS、CSV、ODS、SVG、EMF、XLSM

Document conversion may be commonly used in work. In this blog, we will introduce the free class library Free Spire.XLS for .NET to convert Excel documents into documents in various formats, such as PDF, Image, HTML, TXT, XML, XPS, CSV, ODS, SVG, EMF, XLSM, etc.

This article is transferred from the blog: http://blog.51cto.com/eiceblue/2087681

Class library acquisition address: Free Spire.XLS for .NET (The dll file is obtained in the Bin folder under the installation path)

1. Excel to PDF

 Convert the entire Excel document to PDF here, or you can convert the specified worksheet to PDF

//Create a Wordbook class object and load the Excel document that needs to be converted
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx",ExcelVersion.Version2010);

// get the first worksheet
//Worksheet sheet = workbook.Worksheets[0];

//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");

 Test effect

1. Rotate the entire document


 

2. Transfer to the specified worksheet



 

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 sheet as an image
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToImage("sample.jpg");

 Test effect:



 

2.2 Convert the specified range of cells 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 sheet in the document
Worksheet sheet = workbook.Worksheets[0];

//Specify the cell range and save it as the desired image 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);

 Test effect:



 

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");

 Test effect:



 

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);

 Test effect:




 
 

5. Excel to XML

Workbook wb = new Workbook();
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\sample1.xlsx");
wb.SaveAsXml("result.xml");

 Test effect:



 

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);

 Test effect:



 

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);

 Test effect:

 

8. Excel to ods

 

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
workbook.SaveToFile("Result.ods", FileFormat.ODS);

 Test effect:


 

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 the 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();
         }

 

Test effect:

 

10. Excel to emf

 

//Initialize the Workbbok class instance and load the 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 of the specified size range as Emf format
sheet.SaveToEMFImage("result.emf", 1, 1, 19, 6, System.Drawing.Imaging.EmfType.EmfPlusDual);

 

Test effect:

 

11. Excel to xlsm

 

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx", ExcelVersion.Version97to2003);
workbook.SaveToFile("result.xlsm", FileFormat.Version2007);

 Test effect:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326127158&siteId=291194637