冰蓝科技
|
028-81705109
|
|
微信扫一扫
|

Spire.Cloud 纯前端文档控件

Tab 1

此 Demo 展示如何创建 Excel 图表。

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

using System.Data;
using Spire.Xls;

namespace DemoOnlineCode
{
    class Charts
    {
        public void demoChart(DataTable table, ExcelChartType chartFormat, string resultFileName)
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets[0];
            sheet.InsertDataTable(table, true, 1, 1);
            SetChart(sheet, chartFormat);
            sheetStyle(book, sheet);
            book.SaveToFile(resultFileName,ExcelVersion.Version2010);

        }
        private void SetChart(Worksheet sheet, ExcelChartType chartFormat)
        {
            sheet.Name = "Chart data";
            sheet.GridLinesVisible = false;

            //Writes chart data
            //CreateChartData(sheet);
            //Add a new  chart worsheet to workbook
            Chart chart = sheet.Charts.Add();

            //Set region of chart data

            chart.DataRange = sheet.Range["A1:C7"];
            chart.SeriesDataFromRange = false;

            //Set position of chart
            chart.LeftColumn = 1;
            chart.TopRow = 8;
            chart.RightColumn = 11;
            chart.BottomRow = 29;
            chart.ChartType = chartFormat;


            //Chart title
            chart.ChartTitle = "Sales market by country";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;

            chart.PrimaryCategoryAxis.Title = "Country";
            chart.PrimaryCategoryAxis.Font.IsBold = true;
            chart.PrimaryCategoryAxis.TitleArea.IsBold = true;

            chart.PrimaryValueAxis.Title = "Sales(in Dollars)";
            chart.PrimaryValueAxis.HasMajorGridLines = false;
            chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
            chart.PrimaryValueAxis.MinValue = 1000;
            chart.PrimaryValueAxis.TitleArea.IsBold = true;


            foreach (Spire.Xls.Charts.ChartSerie cs in chart.Series)
            {
                cs.Format.Options.IsVaryColor = true;
                cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
            }

            chart.Legend.Position = LegendPositionType.Top;

        }
        public static void sheetStyle(Workbook book, Worksheet sheet)
        {
            CellStyle oddStyle = book.Styles.Add("oddStyle");
            oddStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            oddStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
            oddStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            oddStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
            oddStyle.KnownColor = ExcelColors.LightGreen1;

            CellStyle evenStyle = book.Styles.Add("evenStyle");
            evenStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            evenStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
            evenStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            evenStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
            evenStyle.KnownColor = ExcelColors.LightTurquoise;


            foreach (CellRange range in sheet.AllocatedRange.Rows)
            {
                if (range.Row % 2 == 0)
                    range.CellStyleName = evenStyle.Name;
                else
                    range.CellStyleName = oddStyle.Name;
            }

            //Sets header style
            CellStyle styleHeader = sheet.Rows[0].Style;
            styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
            styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
            styleHeader.VerticalAlignment = VerticalAlignType.Center;
            styleHeader.KnownColor = ExcelColors.Green;
            styleHeader.Font.KnownColor = ExcelColors.White;
            styleHeader.Font.IsBold = true;

            sheet.Columns[sheet.AllocatedRange.LastColumn - 1].Style.NumberFormat = "\"$\"#,##0";
            sheet.Columns[sheet.AllocatedRange.LastColumn - 2].Style.NumberFormat = "\"$\"#,##0";

            sheet.AllocatedRange.AutoFitColumns();
            sheet.AllocatedRange.AutoFitRows();

            sheet.Rows[0].RowHeight = 20;
        }
    }
}

Tab 3

Imports System.Data
Imports Spire.XLS

Namespace DemoOnlineCode
    Class Charts
        Public Sub demoChart(table As DataTable, chartFormat As ExcelChartType, resultFileName As String)
            Dim book As New Workbook()
            Dim sheet As Worksheet = book.Worksheets(0)
            sheet.InsertDataTable(table, True, 1, 1)
            SetChart(sheet, chartFormat)
            sheetStyle(book, sheet)
            book.SaveToFile(resultFileName, ExcelVersion.Version2010)

        End Sub
        Private Sub SetChart(sheet As Worksheet, chartFormat As ExcelChartType)
            sheet.Name = "Chart data"
            sheet.GridLinesVisible = False

            'Writes chart data
            'CreateChartData(sheet);
            'Add a new  chart worsheet to workbook
            Dim chart As Chart = sheet.Charts.Add()

            'Set region of chart data

            chart.DataRange = sheet.Range("A1:C7")
            chart.SeriesDataFromRange = False

            'Set position of chart
            chart.LeftColumn = 1
            chart.TopRow = 8
            chart.RightColumn = 11
            chart.BottomRow = 29
            chart.ChartType = chartFormat


            'Chart title
            chart.ChartTitle = "Sales market by country"
            chart.ChartTitleArea.IsBold = True
            chart.ChartTitleArea.Size = 12

            chart.PrimaryCategoryAxis.Title = "Country"
            chart.PrimaryCategoryAxis.Font.IsBold = True
            chart.PrimaryCategoryAxis.TitleArea.IsBold = True

            chart.PrimaryValueAxis.Title = "Sales(in Dollars)"
            chart.PrimaryValueAxis.HasMajorGridLines = False
            chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90
            chart.PrimaryValueAxis.MinValue = 1000
            chart.PrimaryValueAxis.TitleArea.IsBold = True


            For Each cs As Spire.Xls.Charts.ChartSerie In chart.Series
                cs.Format.Options.IsVaryColor = True
                cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = True
            Next

            chart.Legend.Position = LegendPositionType.Top

        End Sub
        Public Shared Sub sheetStyle(book As Workbook, sheet As Worksheet)
            Dim oddStyle As CellStyle = book.Styles.Add("oddStyle")
            oddStyle.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin
            oddStyle.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin
            oddStyle.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin
            oddStyle.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin
            oddStyle.KnownColor = ExcelColors.LightGreen1

            Dim evenStyle As CellStyle = book.Styles.Add("evenStyle")
            evenStyle.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin
            evenStyle.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin
            evenStyle.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin
            evenStyle.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin
            evenStyle.KnownColor = ExcelColors.LightTurquoise


            For Each range As CellRange In sheet.AllocatedRange.Rows
                If range.Row Mod 2 = 0 Then
                    range.CellStyleName = evenStyle.Name
                Else
                    range.CellStyleName = oddStyle.Name
                End If
            Next

            'Sets header style
            Dim styleHeader As CellStyle = sheet.Rows(0).Style
            styleHeader.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin
            styleHeader.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin
            styleHeader.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin
            styleHeader.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin
            styleHeader.VerticalAlignment = VerticalAlignType.Center
            styleHeader.KnownColor = ExcelColors.Green
            styleHeader.Font.KnownColor = ExcelColors.White
            styleHeader.Font.IsBold = True

            sheet.Columns(sheet.AllocatedRange.LastColumn - 1).Style.NumberFormat = """$""#,##0"
            sheet.Columns(sheet.AllocatedRange.LastColumn - 2).Style.NumberFormat = """$""#,##0"

            sheet.AllocatedRange.AutoFitColumns()
            sheet.AllocatedRange.AutoFitRows()

            sheet.Rows(0).RowHeight = 20
        End Sub
    End Class
End Namespace

Tab 1

此 Demo 展示如何将 Excel 文档 (xls/xlsx) 转换为 PDF、HTML 和图片格式。

Upload

Maximum file size: 1 MB. Files accepted: xls, xlsx, xlsb, ods.
Click here to browse files.
fileerrors

Convert to

Source file:
filename
Target file type:

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

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

Tab 3

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


Tab 1

此 Demo 展示如何使用邮件合并填充数据到 Word 模板。

Agreement Start Date:
Agreement End Date:
Agreement Extension Date:
Documentation Start Date:
Documentation End Date:
downloads

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

using System;
using Spire.Doc;

namespace DemoOnlineCode
{
    class MailMerge
    {
        public void demoMailMerge(String docFile)
        {
            Document document = new Document(docFile, FileFormat.Auto);

            string[] values = {
                                  DateTime.Today.AddYears(-5).ToString("yyyy-MM-dd"),
                                  DateTime.Today.AddYears(5).ToString("yyyy-MM-dd"),
                                  DateTime.Today.AddYears(6).ToString("yyyy-MM-dd"),
                                  DateTime.Today.AddYears(-2).ToString("yyyy-MM-dd"),
                                  DateTime.Today.AddYears(2).ToString("yyyy-MM-dd")
                              };
            string[] fields = {
                                   "SubGrantPAStartDateValue",
                                   "SubGrantPAEndDateValue",
                                   "SubGrantPAExtensionDateValue",
                                   "SubGrantPSStartDateValue",
                                   "SubGrantPSEndDateValue"
                                 };
            document.MailMerge.Execute(fields, values);
            document.SaveToFile("demo.doc", FileFormat.Doc);

        }
    }
}

Tab 3

Imports Spire.Doc

Namespace DemoOnlineCode
    Class MailMerge
        Public Sub demoMailMerge(docFile As [String])
            Dim document As New Document(docFile, FileFormat.Auto)
            Dim values As String() = {
                                      DateTime.Today.AddYears(-5).ToString("yyyy-MM-dd"),
                                      DateTime.Today.AddYears(5).ToString("yyyy-MM-dd"),
                                      DateTime.Today.AddYears(6).ToString("yyyy-MM-dd"),
                                      DateTime.Today.AddYears(-2).ToString("yyyy-MM-dd"),
                                      DateTime.Today.AddYears(2).ToString("yyyy-MM-dd")
                                  }
            Dim fields As String() = {
                "SubGrantPAStartDateValue",
                "SubGrantPAEndDateValue",
                "SubGrantPAExtensionDateValue",
                "SubGrantPSStartDateValue",
                "SubGrantPSEndDateValue"
                                      }
            document.MailMerge.Execute(fields, values)
            document.SaveToFile("demo.doc", FileFormat.Doc)
        End Sub
    End Class
End Namespace

Tab 1

此 Demo 展示如何在 Word 文档中创建带数据的表格,以及如何设置表格的边框和背景颜色。

Data

Name Capital Continent Area Population Flag
ArgentinaBuenos AiresSouth America277781532300003
BoliviaLa PazSouth America10985757300000
BrazilBrasiliaSouth America8511196150400000
CanadaOttawaNorth America997614726500000
ChileSantiagoSouth America75694313200000
ColombiaBagotaSouth America113890733000000
CubaHavanaNorth America11452410600000
EcuadorQuitoSouth America45550210600000
El SalvadorSan SalvadorNorth America208655300000
GuyanaGeorgetownSouth America214969800000

Option

Header Back Color:
Row Back Color:
Alternation Back Color:
Border Color:
downloads

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

using System;
using System.Data;
using System.Drawing;

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Formatting;


namespace DemoOnlineCode
{
    class Table
    {
        public void demoTable(String docFile, DataTable dataTable,
            string borderColor = null,
            string headerBackColor = null,
            string rowBackColor = null,
            string alternationRowColor = null)
        {
            Document document = new Document(docFile, FileFormat.Auto);
            addTable(document.Sections[0], dataTable,
               ConvertStrToColor(borderColor),
               ConvertStrToColor(headerBackColor),
               ConvertStrToColor(rowBackColor),
               ConvertStrToColor(alternationRowColor));
            document.SaveToFile("demo.doc", FileFormat.Doc);
        }

        private void addTable(Section section,
            DataTable dataTable,
            Color borderColor,
            Color headerBackColor,
            Color rowBackColor,
            Color alternationRowColor)
        {
            Spire.Doc.Table table = section.AddTable();
            int rowCount = dataTable.Rows.Count;
            int columnCount = dataTable.Columns.Count;
            table.DefaultRowHeight = 25;
            table.DefaultColumnWidth = 0;
            table.ResetCells(rowCount + 1, columnCount);

            table.TableFormat.Borders.Left.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Left.Color = borderColor;

            table.TableFormat.Borders.Top.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Top.Color = borderColor;

            table.TableFormat.Borders.Right.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Right.Color = borderColor;

            table.TableFormat.Borders.Bottom.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Bottom.Color = borderColor;

            table.TableFormat.Borders.Horizontal.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Horizontal.Color = borderColor;

            table.TableFormat.Borders.Vertical.BorderType
                = Spire.Doc.Documents.BorderStyle.Hairline;
            table.TableFormat.Borders.Vertical.Color = borderColor;


            TableRow headerRow = table.Rows[0];
            headerRow.IsHeader = true;

            for (int c = 0; c < columnCount; c++)
            {
                Paragraph p = headerRow.Cells[c].AddParagraph();
                p.Format.HorizontalAlignment = HorizontalAlignment.Center;

                Spire.Doc.Fields.TextRange headerText = p.AppendText(dataTable.Columns[c].ColumnName);
                headerText.CharacterFormat.Bold = true;

                CellFormat cellStyle = headerRow.Cells[c].CellFormat;
                cellStyle.VerticalAlignment = VerticalAlignment.Middle;
                headerRow.Cells[c].CellFormat.BackColor = headerBackColor;
            }

            for (int i = 0; i < rowCount; i++)
            {
                object[] rowContent = dataTable.Rows[i].ItemArray;
                DataRow row = dataTable.Rows[i];

                for (int j = 0; j < columnCount; j++)
                {
                    Paragraph p = table.Rows[i + 1].Cells[j].AddParagraph();

                    if (rowContent[j] is byte[])
                    {
                        p.Format.HorizontalAlignment = HorizontalAlignment.Center;
                        p.AppendPicture(rowContent[j] as byte[]);
                    }
                    else
                    {
                        p.AppendText(rowContent[j].ToString());
                    }

                    CellFormat cellStyle = table.Rows[i + 1].Cells[j].CellFormat;
                    cellStyle.VerticalAlignment = VerticalAlignment.Middle;

                    cellStyle.BackColor = rowBackColor;
                    if (i % 2 == 1 && alternationRowColor != Color.Empty)
                    {
                        cellStyle.BackColor = alternationRowColor;
                    }
                }
            }
        }

        private Color ConvertStrToColor(string strColor)
        {
            if (String.IsNullOrWhiteSpace(strColor))
            {
                return Color.Empty;
            }
            else
            {
                return ColorTranslator.FromHtml("#" + strColor);
            }
        }
    }
}

Tab 3

Imports System.Data
Imports System.Drawing

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Formatting


Namespace DemoOnlineCode
    Class Table
        Public Sub demoTable(docFile As [String],
                             dataTable As DataTable,
                             Optional borderColor As String = Nothing,
                             Optional headerBackColor As String = Nothing,
                             Optional rowBackColor As String = Nothing,
                             Optional alternationRowColor As String = Nothing)
            Dim document As New Document(docFile, FileFormat.Auto)
            addTable(document.Sections(0),
                     dataTable, ConvertStrToColor(borderColor),
                     ConvertStrToColor(headerBackColor),
                     ConvertStrToColor(rowBackColor),
                     ConvertStrToColor(alternationRowColor))
            document.SaveToFile("demo.doc", FileFormat.Doc)
        End Sub
        Private Sub addTable(section As Section,
                             dataTable As DataTable,
                             borderColor As Color,
                             headerBackColor As Color,
                             rowBackColor As Color,
                             alternationRowColor As Color)
            Dim table As Spire.Doc.Table = section.AddTable()
            Dim rowCount As Integer = dataTable.Rows.Count
            Dim columnCount As Integer = dataTable.Columns.Count
            table.DefaultRowHeight = 25
            table.DefaultColumnWidth = 0
            table.ResetCells(rowCount + 1, columnCount)

            table.TableFormat.Borders.Left.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Left.Color = borderColor

            table.TableFormat.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Top.Color = borderColor

            table.TableFormat.Borders.Right.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Right.Color = borderColor

            table.TableFormat.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Bottom.Color = borderColor

            table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Horizontal.Color = borderColor

            table.TableFormat.Borders.Vertical.BorderType = Spire.Doc.Documents.BorderStyle.Hairline
            table.TableFormat.Borders.Vertical.Color = borderColor


            Dim headerRow As TableRow = table.Rows(0)
            headerRow.IsHeader = True

            For c As Integer = 0 To columnCount - 1
                Dim p As Paragraph = headerRow.Cells(c).AddParagraph()
                p.Format.HorizontalAlignment = HorizontalAlignment.Center

                Dim headerText As Spire.Doc.Fields.TextRange _
                    = p.AppendText(dataTable.Columns(c).ColumnName)
                headerText.CharacterFormat.Bold = True

                Dim cellStyle As CellFormat = headerRow.Cells(c).CellFormat
                cellStyle.VerticalAlignment = VerticalAlignment.Middle
                headerRow.Cells(c).CellFormat.BackColor = headerBackColor
            Next
            For i As Integer = 0 To rowCount - 1
                Dim rowContent As Object() = dataTable.Rows(i).ItemArray
                Dim row As DataRow = dataTable.Rows(i)

                For j As Integer = 0 To columnCount - 1
                    Dim p As Paragraph = table.Rows(i + 1).Cells(j).AddParagraph()

                    If TypeOf rowContent(j) Is Byte() Then
                        p.Format.HorizontalAlignment = HorizontalAlignment.Center
                        p.AppendPicture(TryCast(rowContent(j), Byte()))
                    Else
                        p.AppendText(rowContent(j).ToString())
                    End If

                    Dim cellStyle As CellFormat = table.Rows(i + 1).Cells(j).CellFormat
                    cellStyle.VerticalAlignment = VerticalAlignment.Middle

                    cellStyle.BackColor = rowBackColor
                    If i Mod 2 = 1 AndAlso alternationRowColor <> Color.Empty Then
                        cellStyle.BackColor = alternationRowColor
                    End If
                Next
            Next
        End Sub
        Private Function ConvertStrToColor(strColor As String) As Color
            If [String].IsNullOrWhiteSpace(strColor) Then
                Return Color.Empty
            Else
                Return ColorTranslator.FromHtml("#" & strColor)
            End If
        End Function
    End Class
End Namespace

Tab 1

此 Demo 展示如何设置 Word 文档的页面大小、页眉和页脚,以及如何将图片设置为页眉/页脚的背景。

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

using System;
using System.Drawing;

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace DemoOnlineCode
{
    class HeaderAndFooter
    {
        public void demoHeaderAndFooter(String docFile,
            String headerImageFile = null,
            String footerImageFile = null,
            string pageSizeName = "A4",
            String headerFontColor = null,
            String footerFontColor = null)
        {
            Document document = new Document(docFile, FileFormat.Auto);
            SizeF pageSize = (SizeF)typeof(PageSize).GetField(pageSizeName).GetValue(null);
            PageSetup(document, pageSize);
            SetHeader(document, ConvertStrToColor(headerFontColor), headerImageFile);
            SetFooter(document,ConvertStrToColor(footerFontColor), footerImageFile);
            document.SaveToFile("demo.doc", FileFormat.Doc);

        }

        private void PageSetup(Document document, SizeF pageSize)
        {
            for (int i = 0; i < document.Sections.Count; i++)
            {
                Section section = document.Sections[i];
                section.PageSetup.PageSize = pageSize;
                section.PageSetup.Margins.Top = (float)72.0;
                section.PageSetup.Margins.Bottom = (float)72.0;
                section.PageSetup.Margins.Left = (float)89.0;
                section.PageSetup.Margins.Right = (float)89.0;
            }
        }

        private void SetFooter(Document document,
            Color fontColor,
            string footerImageFile = null,
            string footerText = "Spire.Doc",
            string fontName = "Calibri",
            float fontSize = (float)12.0,
            HorizontalAlignment alignment = HorizontalAlignment.Left)
        {
            for (int i = 0; i < document.Sections.Count; i++)
            {
                Section section = document.Sections[i];
                HeaderFooter footer = section.HeadersFooters.Footer;
                Paragraph footerParagraph = footer.AddParagraph();
                TextRange text = footerParagraph.AppendText(footerText);

                text.CharacterFormat.FontName = fontName;
                text.CharacterFormat.FontSize = fontSize;
                text.CharacterFormat.TextColor = fontColor;
                footerParagraph.Format.HorizontalAlignment = alignment;

                //border
                footerParagraph.Format.Borders.Top.BorderType
                    = (BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
                footerParagraph.Format.Borders.Top.Space = (float)0.03;
                if (footerImageFile != null)
                {
                    Byte[] imagedata = System.IO.File.ReadAllBytes(footerImageFile);
                    //insert picture to footer
                    DocPicture footerPicture = footerParagraph.AppendPicture(imagedata);

                    //footer picture layout
                    footerPicture.TextWrappingStyle = TextWrappingStyle.Behind;
                    footerPicture.HorizontalOrigin = HorizontalOrigin.Page;
                    footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
                    footerPicture.VerticalOrigin = VerticalOrigin.Page;
                    footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom;

                }
                //insert page number
                footerParagraph = footer.AddParagraph();
                footerParagraph.AppendField("page number", FieldType.FieldPage);
                footerParagraph.AppendText("of");
                footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
                footerParagraph.Format.HorizontalAlignment
                    = (HorizontalAlignment)HorizontalAlignment.Right;
            }
        }

        private void SetHeader(Document document,
            Color fontColor,
            string headerImageFile = null,
            string headerText = "E-iceblue",
            string fontName = "Calibri",
            float fontSize = (float)12.0,
            HorizontalAlignment alignment = HorizontalAlignment.Center)
        {
            for (int i = 0; i < document.Sections.Count; i++)
            {
                Section section = document.Sections[i];
                HeaderFooter header = section.HeadersFooters.Header;

                //insert picture and text to header
                Paragraph headerParagraph = header.AddParagraph();
                if (headerImageFile != null)
                {
                    Byte[] imagedata = System.IO.File.ReadAllBytes(headerImageFile);
                    DocPicture headerPicture = headerParagraph.AppendPicture(imagedata);

                    //header picture layout - text wrapping
                    headerPicture.TextWrappingStyle = TextWrappingStyle.Behind;

                    //header picture layout - position
                    headerPicture.HorizontalOrigin = HorizontalOrigin.Page;
                    headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
                    headerPicture.VerticalOrigin = VerticalOrigin.Page;
                    headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top;
                }

                //header text
                TextRange text = headerParagraph.AppendText(headerText);
                text.CharacterFormat.FontName = fontName;
                text.CharacterFormat.FontSize = Convert.ToSingle(fontSize);
                text.CharacterFormat.TextColor = fontColor;
                headerParagraph.Format.HorizontalAlignment = alignment;

                //border
                headerParagraph.Format.Borders.Bottom.BorderType
                    = (Spire.Doc.Documents.BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
                headerParagraph.Format.Borders.Bottom.Space = (float)0.03;

            }
        }

        private Color ConvertStrToColor(string strColor)
        {
            if (String.IsNullOrWhiteSpace(strColor))
            {
                return Color.Empty;
            }
            else
            {
                return ColorTranslator.FromHtml("#" + strColor);
            }
        }

    }
}

Tab 3

Imports System.Drawing

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace DemoOnlineCode
    Class HeaderAndFooter
        Public Sub demoHeaderAndFooter(docFile As [String],
                                       Optional headerImageFile As [String] = Nothing,
                                       Optional footerImageFile As [String] = Nothing,
                                       Optional pageSizeName As String = "A4",
                                       Optional headerFontColor As [String] = Nothing,
                                       Optional footerFontColor As [String] = Nothing)
            Dim document As New Document(docFile, FileFormat.Auto)
            Dim pageSize As SizeF _
                = CType(GetType(PageSize).GetField(pageSizeName).GetValue(Nothing), SizeF)
            PageSetup(document, pageSize)
            SetHeader(document, ConvertStrToColor(headerFontColor), headerImageFile)
            SetFooter(document, ConvertStrToColor(footerFontColor), footerImageFile)
            document.SaveToFile("demo.doc", FileFormat.Doc)

        End Sub
        Private Sub PageSetup(document As Document, pageSize As SizeF)
            For i As Integer = 0 To document.Sections.Count - 1
                Dim section As Section = document.Sections(i)
                section.PageSetup.PageSize = pageSize
                section.PageSetup.Margins.Top = CSng(72.0)
                section.PageSetup.Margins.Bottom = CSng(72.0)
                section.PageSetup.Margins.Left = CSng(89.0)
                section.PageSetup.Margins.Right = CSng(89.0)
            Next
        End Sub
        Private Sub SetFooter(document As Document,
                              fontColor As Color,
                              Optional footerImageFile As String = Nothing,
                              Optional footerText As String = "Spire.Doc",
                              Optional fontName As String = "Calibri",
                              Optional fontSize As Single = CSng(12.0), _
                              Optional alignment As HorizontalAlignment = HorizontalAlignment.Left)
            For i As Integer = 0 To document.Sections.Count - 1
                Dim section As Section = document.Sections(i)
                Dim footer As HeaderFooter = section.HeadersFooters.Footer
                Dim footerParagraph As Paragraph = footer.AddParagraph()
                Dim text As TextRange = footerParagraph.AppendText(footerText)

                text.CharacterFormat.FontName = fontName
                text.CharacterFormat.FontSize = fontSize
                text.CharacterFormat.TextColor = fontColor
                footerParagraph.Format.HorizontalAlignment = alignment

                'border
                footerParagraph.Format.Borders.Top.BorderType _
                    = DirectCast(Spire.Doc.Documents.BorderStyle.Hairline, BorderStyle)
                footerParagraph.Format.Borders.Top.Space = CSng(0.03)
                If footerImageFile IsNot Nothing Then
                    Dim imagedata As [Byte]() = System.IO.File.ReadAllBytes(footerImageFile)
                    'insert picture to footer
                    Dim footerPicture As DocPicture = footerParagraph.AppendPicture(imagedata)

                    'footer picture layout
                    footerPicture.TextWrappingStyle = TextWrappingStyle.Behind
                    footerPicture.HorizontalOrigin = HorizontalOrigin.Page
                    footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
                    footerPicture.VerticalOrigin = VerticalOrigin.Page

                    footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom
                End If
                'insert page number
                footerParagraph = footer.AddParagraph()
                footerParagraph.AppendField("page number", FieldType.FieldPage)
                footerParagraph.AppendText("of")
                footerParagraph.AppendField("number of pages", FieldType.FieldNumPages)
                footerParagraph.Format.HorizontalAlignment _
                    = DirectCast(HorizontalAlignment.Right, HorizontalAlignment)
            Next
        End Sub
        Private Sub SetHeader(document As Document,
                              fontColor As Color,
                              Optional headerImageFile As String = Nothing,
                              Optional headerText As String = "E-iceblue",
                              Optional fontName As String = "Calibri",
                              Optional fontSize As Single = CSng(12.0), _
         Optional alignment As HorizontalAlignment = HorizontalAlignment.Center)
            For i As Integer = 0 To document.Sections.Count - 1
                Dim section As Section = document.Sections(i)
                Dim header As HeaderFooter = section.HeadersFooters.Header

                'insert picture and text to header
                Dim headerParagraph As Paragraph = header.AddParagraph()
                If headerImageFile IsNot Nothing Then
                    Dim imagedata As [Byte]() = System.IO.File.ReadAllBytes(headerImageFile)
                    Dim headerPicture As DocPicture = headerParagraph.AppendPicture(imagedata)

                    'header picture layout - text wrapping
                    headerPicture.TextWrappingStyle = TextWrappingStyle.Behind

                    'header picture layout - position
                    headerPicture.HorizontalOrigin = HorizontalOrigin.Page
                    headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
                    headerPicture.VerticalOrigin = VerticalOrigin.Page
                    headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top
                End If

                'header text
                Dim text As TextRange = headerParagraph.AppendText(headerText)
                text.CharacterFormat.FontName = fontName
                text.CharacterFormat.FontSize = Convert.ToSingle(fontSize)
                text.CharacterFormat.TextColor = fontColor
                headerParagraph.Format.HorizontalAlignment = alignment

                'border
                headerParagraph.Format.Borders.Bottom.BorderType _
                    = DirectCast(BorderStyle.Hairline, Spire.Doc.Documents.BorderStyle)

                headerParagraph.Format.Borders.Bottom.Space = CSng(0.03)
            Next
        End Sub
        Private Function ConvertStrToColor(strColor As String) As Color
            If [String].IsNullOrWhiteSpace(strColor) Then
                Return Color.Empty
            Else
                Return ColorTranslator.FromHtml("#" & strColor)
            End If
        End Function
    End Class
End Namespace

Tab 1

此 Demo 展示如何在 Word 文档中搜索文本并高亮显示匹配的文本。

Upload

Maximum file size: 1 MB. Files accepted: doc, docx, txt, rtf.
Click here to browse files.
fileerrors

Convert to

Source file:
filename
Search Text:

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace DemoOnlineCode
{
    class FindAndHighlight
    {
        public void demoFindAndHighlight(String docFile, String searchText)
        {
            Document document = new Document(docFile, FileFormat.Auto);

            TextSelection[] textSelections
                = document.FindAllString(searchText, false, false);
            if (textSelections != null)
            {
                foreach (TextSelection selection in textSelections)
                {
                    TextRange textMatched = selection.GetAsOneRange();
                    textMatched.CharacterFormat.HighlightColor = Color.Yellow;
                }
            }

            document.SaveToFile("demo.doc", FileFormat.Doc);
        }
    }
}

Tab 3

Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace DemoOnlineCode
    Class FindAndHighlight
        Public Sub demoFindAndHighlight(docFile As [String], searchText As [String])
            Dim document As New Document(docFile, FileFormat.Auto)

            Dim textSelections As TextSelection() = document.FindAllString(searchText, False, False)
            If textSelections IsNot Nothing Then
                For Each selection As TextSelection In textSelections
                    Dim textMatched As TextRange = selection.GetAsOneRange()
                    textMatched.CharacterFormat.HighlightColor = Color.Yellow
                Next
            End If

            document.SaveToFile("demo.doc", FileFormat.Doc)
        End Sub
    End Class
End Namespace

Tab 1

此 Demo 展示如何将 Word 文档 (doc/docx) 转换为 PDF、HTML、Image、XPS、RTF 和 TIFF 文件格式。

Upload

Maximum file size: 1 MB. Files accepted: doc, docx, txt, rtf.
Click here to browse files.
fileerrors

Convert to

Source file:
filename
Target file type:

如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo

如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。

Tab 2

using System;
using System.Drawing;
using System.Drawing.Imaging;

using Spire.Doc;
using Spire.Doc.Documents;

namespace DemoOnlineCode
{
    class Convertors
    {
        public void demoConversion(String docFile, string format)
        {
            Document document = new Document(docFile, FileFormat.Auto);
            ConvertFormat(document, format);
        }

        private void ConvertFormat(Document document, string format)
        {
            switch (format)
            {
                case "PDF":
                    document.SaveToFile("demo.pdf", FileFormat.PDF);
                    break;

                case "XPS":
                    document.SaveToFile("demo.xps", FileFormat.XPS);
                    break;

                case "Image":
                    Image[] Images = document.SaveToImages(ImageType.Bitmap);
                    if (Images != null && Images.Length > 0)
                    {
                        if (Images.Length == 1)
                        {
                            Images[0].Save("demo.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                        }
                        else
                        {
                            for (int i = 0; i < Images.Length; i++)
                            {
                                String fileName = String.Format("Image-{0}.png", i);
                                Images[i].Save(fileName, ImageFormat.Png);
                            }
                        }
                    }
                    break;
                case "RTF":
                    document.SaveToFile("demo.rtf", FileFormat.Rtf);
                    break;
                case "TIFF":
                    Image[] TiffImage = document.SaveToImages(ImageType.Bitmap);

                    Image result = JoinTiffImages(TiffImage, EncoderValue.CompressionLZW);
                    result.Save("demo.tiff", ImageFormat.Tiff);
                    break;
                case "HTML":
                    document.SaveToFile("demo.html", FileFormat.Html);
                    break;
            }
        }

        private static Image JoinTiffImages(Image[] images, EncoderValue compressEncoder)
        {

            //use the save encoder
            Encoder enc = Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);

            Image pages = null;
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");

            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;
                    pages.Save("demo.tiff", info, ep);
                }
                else
                {
                    //save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);

                }

                if ((frame == images.Length - 1) && frame != 0)
                {
                    //flush and close.
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }

                frame++;
            }
            return pages;
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }

            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }
    }

}

Tab 3

Imports System.Drawing
Imports System.Drawing.Imaging

Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace DemoOnlineCode
    Class Convertors
        Public Sub demoConversion(docFile As [String], format As String)
            Dim document As New Document(docFile, FileFormat.Auto)
            ConvertFormat(document, format)
        End Sub

        Private Sub ConvertFormat(document As Document, format As String)
            Select Case format
                Case "PDF"
                    document.SaveToFile("demo.pdf", FileFormat.PDF)
                    Exit Select

                Case "XPS"
                    document.SaveToFile("demo.xps", FileFormat.XPS)
                    Exit Select

                Case "Image"
                    Dim Images As Image() = document.SaveToImages(ImageType.Bitmap)
                    If Images IsNot Nothing AndAlso Images.Length > 0 Then
                        If Images.Length = 1 Then
                            Images(0).Save("demo.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
                        Else
                            For i As Integer = 0 To Images.Length - 1
                                Dim fileName As [String] = [String].Format("Image-{0}.png", i)
                                Images(i).Save(fileName, ImageFormat.Png)
                            Next
                        End If
                    End If
                    Exit Select
                Case "RTF"
                    document.SaveToFile("demo.rtf", FileFormat.Rtf)
                    Exit Select
                Case "TIFF"
                    Dim TiffImage As Image() = document.SaveToImages(ImageType.Bitmap)

                    Dim result As Image = JoinTiffImages(TiffImage, EncoderValue.CompressionLZW)
                    result.Save("demo.tiff", ImageFormat.Tiff)
                    Exit Select
                Case "HTML"
                    document.SaveToFile("demo.html", FileFormat.Html)
                    Exit Select
            End Select
        End Sub

        Private Shared Function JoinTiffImages(images As Image(), _
                                               compressEncoder As EncoderValue) As Image

            'use the save encoder
            Dim enc As Encoder = Encoder.SaveFlag
            Dim ep As New EncoderParameters(2)
            ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.MultiFrame))
            ep.Param(1) = New EncoderParameter(Encoder.Compression, CLng(compressEncoder))

            Dim pages As Image = Nothing
            Dim frame As Integer = 0
            Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff")

            For Each img As Image In images
                If frame = 0 Then
                    pages = img
                    pages.Save("demo.tiff", info, ep)
                Else
                    'save the intermediate frames
                    ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.FrameDimensionPage))
                    pages.SaveAdd(img, ep)
                End If

                If (frame = images.Length - 1) AndAlso frame <> 0 Then
                    'flush and close.
                    ep.Param(0) = New EncoderParameter(enc, CLng(EncoderValue.Flush))
                    pages.SaveAdd(ep)
                End If

                frame += 1
            Next
            Return pages
        End Function

        Private Shared Function GetEncoderInfo(mimeType As String) As ImageCodecInfo
            Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
            For j As Integer = 0 To encoders.Length - 1
                If encoders(j).MimeType = mimeType Then
                    Return encoders(j)
                End If
            Next

            Throw New Exception(mimeType & " mime type not found in ImageCodecInfo")
        End Function
    End Class

End Namespace

Spire.Presentation for Java 4.8.4已发布。本次更新修复了给PPTX文档添加批注时出现的问题,同时还修复了添加的项目列表符号不正确的问题。详情请阅读以下内容。

问题修复:


获取Spire.Presentation for Java 4.8.4请点击:

https://www.e-iceblue.cn/Downloads/Spire-Presentation-JAVA.html

本文将介绍如何使用Spire.XLS for Java设置Excel单元格文字自动换行和取消自动换行。

原Excel文档:

Java 设置 Excel 单元格文字自动换行和取消自动换行

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class WrapOrUnwrapText {
    public static void main(String []args) throws Exception {
        //创建Workbook实例
        Workbook workbook = new Workbook();
        //加载Excel文档
        workbook.loadFromFile("Input.xlsx");

        //获取第一张工作表
        Worksheet sheet = workbook.getWorksheets().get(0);

        //设置单元格"A1"的文字自动换行
        sheet.getRange().get("A1").getStyle().setWrapText(true);

        //取消单元格"A6"的文字自动换行
        sheet.getRange().get("A6").getStyle().setWrapText(false);

        //保存文档
        workbook.saveToFile("WrapOrUnwrapText.xlsx", ExcelVersion.Version2013);
    }
}

结果Excel文档:

Java 设置 Excel 单元格文字自动换行和取消自动换行

本文介绍使用Spire.XLS for .NET来压缩Excel工作表中的图片。

C#
using Spire.Xls;

namespace CompressImgs
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载Excel测试文档
            Workbook wb = new Workbook();
            wb.LoadFromFile("test.xlsx");

            //遍历工作表
            foreach (Worksheet sheet in wb.Worksheets)
            {
                //遍历工作表中的所有图片
                foreach (ExcelPicture picture in sheet.Pictures)
                {
                    picture.Compress(30);//压缩图片
                }
            }

            //保存文档
            wb.SaveToFile("CompressImgs.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("CompressImgs.xlsx");
        }
    }
}
VB.NET
Imports Spire.Xls

Namespace CompressImgs
	Class Program
		Private Shared Sub Main(args As String())
			'加载Excel测试文档
			Dim wb As New Workbook()
			wb.LoadFromFile("test.xlsx")

			'遍历工作表
			For Each sheet As Worksheet In wb.Worksheets
				'遍历工作表中的所有图片
				For Each picture As ExcelPicture In sheet.Pictures
						'压缩图片
					picture.Compress(30)
				Next
			Next

			'保存文档
			wb.SaveToFile("CompressImgs.xlsx", ExcelVersion.Version2013)
			System.Diagnostics.Process.Start("CompressImgs.xlsx")
		End Sub
	End Class
End Namespace

压缩结果:

C#/VB.NET 压缩 Excel 中的图片