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

Spire.Cloud 纯前端文档控件

该文将介绍如何同时使用Spire.Doc和Spire.XLS ,将Excel 工作表里面的数据,含字体,样式,背景色等复制到Word表格中。

首先,请查看Excel示例文档:

Java 将 Excel 数据保存为 Word 表格

import com.spire.doc.Document;
import com.spire.doc.Table;
import com.spire.doc.TableCell;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.VerticalAlignment;
import com.spire.doc.fields.TextRange;
import com.spire.xls.CellRange;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ExcelToWordTable {
    public static void main(String[] args) throws Exception {
        //加载Excel 示例文档
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample00.xlsx");
        //获取第一个工作表
        Worksheet sheet = workbook.getWorksheets().get(0);

        //复制到Word文档
        copyToWord(sheet.getAllocatedRange(), "output.docx");

    }

    public static void copyToWord(CellRange cell, String fPath) {
        //添加表格
        Document doc = new Document();
        Table table = doc.addSection().addTable(true);
        table.resetCells(cell.getRowCount(), cell.getColumnCount());
        //复制表格内容
        for (int r = 1; r <= cell.getRowCount(); r++) {
            for (int c = 1; c <= cell.getColumnCount(); c++) {
                CellRange xCell = cell.get(r, c);
                CellRange mergeArea = xCell.getMergeArea();
                //合并单元格
                if (mergeArea != null && mergeArea.getRow() == r && mergeArea.getColumn() == c) {
                    int rowIndex = mergeArea.getRow();
                    int columnIndex = mergeArea.getColumn();
                    int rowCount = mergeArea.getRowCount();
                    int columnCount = mergeArea.getColumnCount();

                    for (int m = 0; m < rowCount; m++) {
                        table.applyHorizontalMerge(rowIndex - 1 + m, columnIndex - 1, columnIndex + columnCount - 2);
                    }
                    table.applyVerticalMerge(columnIndex - 1, rowIndex - 1, rowIndex + rowCount - 2);
                }
                //复制内容
                TableCell wCell = table.getRows().get(r - 1).getCells().get(c - 1);
                if (!xCell.getDisplayedText().isEmpty()) {
                    TextRange textRange = wCell.addParagraph().appendText(xCell.getDisplayedText());
                    copyStyle(textRange, xCell, wCell);
                } else {
                    wCell.getCellFormat().setBackColor(xCell.getStyle().getColor());
                }
            }
        }
        doc.saveToFile(fPath,com.spire.doc.FileFormat.Docx);
}
        private static void copyStyle(TextRange wTextRange, CellRange xCell, TableCell wCell) {
            //复制字体样式
            wTextRange.getCharacterFormat().setTextColor(xCell.getStyle().getFont().getColor());
            wTextRange.getCharacterFormat().setFontSize((float) xCell.getStyle().getFont().getSize());
            wTextRange.getCharacterFormat().setFontName(xCell.getStyle().getFont().getFontName());
            wTextRange.getCharacterFormat().setBold(xCell.getStyle().getFont().isBold());
            wTextRange.getCharacterFormat().setItalic(xCell.getStyle().getFont().isItalic());
            //复制背景色
            wCell.getCellFormat().setBackColor(xCell.getStyle().getColor());
            //复制排列方式
            switch (xCell.getHorizontalAlignment()) {
                case Left:
                    wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
                    break;
                case Center:
                    wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                    break;
                case Right:
                    wTextRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
                    break;
                default:
                    break;
            }
            switch (xCell.getVerticalAlignment()) {
                case Bottom:
                    wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Bottom);
                    break;
                case Center:
                    wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                    break;
                case Top:
                    wCell.getCellFormat().setVerticalAlignment(VerticalAlignment.Top);
                    break;
                default:
                    break;
            }
        }
    }

效果图:

Java 将 Excel 数据保存为 Word 表格

本文将介绍如何使用Spire.XLS for Java创建含多层分类标签 Excel图表。

import com.spire.xls.*;
import com.spire.xls.charts.*;

public class CreateMultiLevelChart {
    public static void main(String []args) throws Exception {
        //创建Workbook实例,获取第一个工作表
        Workbook wb = new Workbook();
        Worksheet sheet = wb.getWorksheets().get(0);

        //写入数据到工作表
        sheet.getCellRange("A1").setText("年份");
        sheet.getCellRange("A2").setText("2016年");
        sheet.getCellRange("A6").setText("2017年");
        sheet.getCellRange("B1").setText("季度");
        sheet.getCellRange("B2").setText("第一季度");
        sheet.getCellRange("B3").setText("第二季度");
        sheet.getCellRange("B4").setText("第三季度");
        sheet.getCellRange("B5").setText("第四季度");
        sheet.getCellRange("B6").setText("第一季度");
        sheet.getCellRange("B7").setText("第二季度");
        sheet.getCellRange("B8").setText("第三季度");
        sheet.getCellRange("B9").setText("第四季度");
        sheet.getCellRange("C1").setText("季度销量");
        sheet.getCellRange("C2").setValue("1544");
        sheet.getCellRange("C3").setValue("1102");
        sheet.getCellRange("C4").setValue("2558");
        sheet.getCellRange("C5").setValue("1609");
        sheet.getCellRange("C6").setValue("1700");
        sheet.getCellRange("C7").setValue("1304");
        sheet.getCellRange("C8").setValue("1572");
        sheet.getCellRange("C9").setValue("3210");

        //纵向合并单元格A2到A5、A6到A9
        sheet.getCellRange("A2:A5").merge();
        sheet.getCellRange("A6:A9").merge();

        //添加柱状图
        Chart chart = sheet.getCharts().add(ExcelChartType.ColumnClustered);
        chart.setChartTitle("季度销量");  //设置图标标题
        chart.getPlotArea().getFill().setFillType(ShapeFillType.NoFill);  //不填充绘图区域(默认填充灰色)
        chart.getLegend().delete();  //删除图例

        //设置柱状图位置及宽度
        chart.setLeftColumn(5);
        chart.setTopRow(1);
        chart.setRightColumn(14);

        //设置系列数据来源
        chart.setDataRange(sheet.getCellRange("C2:C9"));
        chart.setSeriesDataFromRange(false);

        //设置系列分类标签数据来源
        ChartSerie serie = chart.getSeries().get(0);
        serie.setCategoryLabels(sheet.getCellRange("A2:B9"));

        //显示多层分类标签
        chart.getPrimaryCategoryAxis().setMultiLevelLable(true);

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

Java 创建含多层分类标签的 Excel 图表

本文介绍使用Spire.Doc for .NET 在Word中添加横线的方法。

C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace AddHorizontalLine
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载Word测试文档
            Document doc = new Document();
            doc.LoadFromFile("https://cdn.e-iceblue.cn/test.docx");

            //获取节和段落
            Section section = doc.Sections[0];
            Paragraph paragraph = section.Paragraphs[4];

            //添加横线
            paragraph.AppendHorizonalLine();

            //保存文档
            doc.SaveToFile("output.docx");
            System.Diagnostics.Process.Start("output.docx");
        }
    }
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace AddHorizontalLine
	Class Program
		Private Shared Sub Main(args As String())
			'加载Word测试文档
			Dim doc As New Document()
			doc.LoadFromFile("https://cdn.e-iceblue.cn/test.docx")

			'获取节和段落
			Dim section As Section = doc.Sections(0)
			Dim paragraph As Paragraph = section.Paragraphs(4)

			'添加横线
			paragraph.AppendHorizonalLine()

			'保存文档
			doc.SaveToFile("output.docx")
			System.Diagnostics.Process.Start("output.docx")

		End Sub
	End Class
End Namespace

横线添加效果:

C#/VB.NET 在 Word 中添加横线

Spire.Doc 9.6已发布。该版本支持添加跨整个页面的横线,增强了转换Word到图片/PDF的功能,此外,本次更新还修复了加载和保存Word文档等时出现的问题。详情请阅读以下内容。

新功能:

问题修复:


获取Spire.Doc 9.6请点击:

https://www.e-iceblue.cn/Downloads/Spire-Doc-NET.html

Spire.XLS 11.6现已正式发布。该版本支持判断指定行或列是否隐藏,以及将Excel到PDF时指定字体目录。同时,也成功修复了将表格转换为HTML、Excel转换为PDF、Excel转换为SVG和加载XLSX文件时出现的一些问题。更多新功能及问题修复详情,请参阅以下内容。

新功能:

问题修复:


下载Spire.XLS 11.6,请点击:

https://www.e-iceblue.cn/Downloads/Spire-XLS-NET.html

Spire.PDF 7.6已发布。本次更新支持了通过流加载SVG文件的功能,增强了转换PDF到图片/SVG的功能。此外,该版本还修复了加密文档等时出现的问题。详情请阅读以下内容。

新功能:

问题修复:


获取Spire.PDF 7.6请点击:

https://www.e-iceblue.cn/Downloads/Spire-PDF-NET.html

前面我们介绍了如何使用 Spire.PDF for Java 将PDF保存为图片。该文将介绍如何在Java应用程序中将图片(Jpg, PNG, Bmp等)保存为PDF。

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;

public class imageToPDF {
    public static void main(String[] args) throws Exception {

        //创建Pdf 文档
        PdfDocument pdf = new PdfDocument();
        //创建一个新的页面
        PdfPageBase page = pdf.getPages().add();

        //加载图片
        PdfImage image = PdfImage.fromFile("logo.jpg");

        double widthFitRate = image.getPhysicalDimension().getWidth() / page.getCanvas().getClientSize().getWidth();
        double heightFitRate = image.getPhysicalDimension().getHeight() / page.getCanvas().getClientSize().getHeight();
        double fitRate = Math.max(widthFitRate, heightFitRate);

        //图片大小
        double fitWidth = image.getPhysicalDimension().getWidth() / fitRate;
        double fitHeight = image.getPhysicalDimension().getHeight() / fitRate;

        //绘制图片
        page.getCanvas().drawImage(image, 0, 30, fitWidth, fitHeight);


        pdf.saveToFile("output/ToPDF.pdf");
        pdf.close();

    }
}

效果图:

Java 转换图片到 PDF

本文介绍如何使用Spire.Doc for Java删除Word文档中的指定段落。

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class RemoveParagraph {

    public static void main(String[] args) {

        //创建Document对象
        Document document = new Document();

        //加载示例文档
        document.loadFromFile("https://cdn.e-iceblue.cn/C:\\Users\\Administrator\\Desktop\\Java Doc.docx");

        //删除第一节的第二段
        document.getSections().get(0).getParagraphs().removeAt(1);

        //保存文档
        document.saveToFile("RemoveParagraph.docx", FileFormat.Docx_2013);
    }
}

Java 删除 Word 中的指定段落

本文介绍如何使用Spire.Presentation for Java来读取PPT幻灯片中的批注内容,包括添加批注的作者、批注内容以及添加批注的时间等。

PPT测试文档如下,含如下批注内容:

Java 获取 PPT 幻灯片中的批注内容

import com.spire.presentation.*;

import java.util.Date;

public class GetComment {
    public static void main(String[] args) throws Exception{
        //加载PPT测试文档
        Presentation ppt = new Presentation();
        ppt.loadFromFile("addcomment.pptx");

        //获取指定幻灯片(第2张)
        ISlide slide = ppt.getSlides().get(1);

        //获取幻灯片中的第一个批注
        Comment comment = slide.getComments()[0];
        String author = comment.getAuthorName();
        String commenttext = comment.getText();
        Date time = comment.getDateTime();

        //输出获取的批注内容
        System.out.println( "批注作者:" + author + "\n"
                        + "添加批注时间:" + time + "\n"
                        + "批注内容:" + commenttext + "\n");
    }
}

批注内容读取结果:

Java 获取 PPT 幻灯片中的批注内容

Spire.Office for Java 4.5.2已发布。本次更新带了一些新的功能,比如,Spire.XLS for Java新增方法支持获取分组框,支持转换Excel到PDF时指定字体目录的功能;Spire.Presentation for Java 新增方法支持通过流添加图片到 PPT。此外,该版本还修复了大量的问题,详情请看以下内容。

获取Spire.Office for Java 4.5.2请点击:

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


Spire.XLS for Java

新功能:

调整:

问题修复:


Spire.PDF for Java

问题修复:


Spire.Presentation for Java

新功能:

问题修复: