Thinkphp5使用phpword生成图表

因为没有用composer安装所以手动添加导extend下引用

笔者在学习的时候出现 class‘xxxx’not found情况,如果各位也出现这情况 请仔细检查文件路径

用composer安装phpword笔者出现无法加载类的问题(没有解决),后用tp5官方的框架手动加载的phpword成功。

文档目录

<?php
namespace app\index\controller;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\TemplateProcessor;
class Index
{
    public function index()
    {
    ;
        //实例化文档
        $PHPWord = new \PhpOffice\PhpWord\PhpWord();
        //添加容器
        $section = $PHPWord->addSection();
        //添加图表数据
        $categories = array('A', 'B', 'C', 'D', 'E');
        $series = array(1, 3, 2, 5, 4);
        //饼图
        $section->addChart('pie', $categories, $series);
        //环图
        $section->addChart('doughnut', $categories, $series);
        //横柱图(字数在上)
        $section->addChart('bar', $categories, $series);
        //横柱图(字数在里)
        $section->addChart('stacked_bar', $categories, $series);
        //横柱图
        $section->addChart('percent_stacked_bar', $categories, $series);
        //竖柱图
        $section->addChart('column', $categories, $series);
        //竖柱图
        $section->addChart('percent_stacked_column', $categories, $series);
        //驼峰图
        $section->addChart('area', $categories, $series);
        //五边形
        $section->addChart('radar', $categories, $series);
        //点图
        $section->addChart('scatter', $categories, $series);
        //线图
        $section->addChart('line', $categories, $series);
        //保存文件后面接文件路径
        $PHPWord->save('C:\Users\Administrator\Desktop\chart.docx');

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
表格格式:<br>http://hi.baidu.com/metaza/blog/item/b277ccc4183b73ab8226ac8b.html<br>http://www.cnblogs.com/cryf/articles/718605.html<br><br>转载自 微软MSDN<br><br>private void button1_Click(object sender, System.EventArgs e)<br>{<br>object oMissing = System.Reflection.Missing.Value;<br>object oEndOfDoc = "\\endofdoc"; <br><br>/* \endofdoc是预定义的bookmark */ <br><br>//创建一个document.<br>Word._Application oWord;<br>Word._Document oDoc;<br>oWord = new Word.Application();<br>oWord.Visible = true;<br>oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,<br>ref oMissing, ref oMissing);<br><br>//在document的开始部分添加一个paragraph.<br>Word.Paragraph oPara1;<br>oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);<br>oPara1.Range.Text = "Heading 1";<br>oPara1.Range.Font.Bold = 1;<br>oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.<br>oPara1.Range.InsertParagraphAfter();<br><br>//在当前document的最后添加一个paragraph<br><br>Word.Paragraph oPara2;<br>object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);<br>oPara2.Range.Text = "Heading 2";<br>oPara2.Format.SpaceAfter = 6;<br>oPara2.Range.InsertParagraphAfter();<br><br>//接着添加一个paragraph<br>Word.Paragraph oPara3;<br>oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);<br>oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";<br>oPara3.Range.Font.Bold = 0;<br>oPara3.Format.SpaceAfter = 24;<br>oPara3.Range.InsertParagraphAfter();<br><br><br>//添加一个3行5列的表格,填充数据,并且设定第一行的样式<br><br>Word.Table oTable;<br>Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);<br>oTable.Range.ParagraphFormat.SpaceAfter = 6;<br>int r, c;<br>string strText;<br>for(r = 1; r <= 3; r++)<br>for(c = 1; c <= 5; c++)<br>{<br>strText = "r" + r + "c" + c;<br>oTable.Cell(r, c).Range.Text = strText;<br>}<br>oTable.Rows[1].Range.Font.Bold = 1;<br>oTable.Rows[1].Range.Font.Italic = 1;<br><br>//接着添加一些文字<br><br>Word.Paragraph oPara4;<br>oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oPara4 = oDoc.Content.Paragraphs.Add(ref oRng);<br>oPara4.Range.InsertParagraphBefore();<br>oPara4.Range.Text = "And here's another table:";<br>oPara4.Format.SpaceAfter = 24;<br>oPara4.Range.InsertParagraphAfter();<br><br><br>//添加一个5行2列的表,填充数据并且改变列宽<br>wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oTable = oDoc.Tables.Add(wrdRng, 5, 2, ref oMissing, ref oMissing);<br>oTable.Range.ParagraphFormat.SpaceAfter = 6;<br>for(r = 1; r <= 5; r++)<br>for(c = 1; c <= 2; c++)<br>{<br>strText = "r" + r + "c" + c;<br>oTable.Cell(r, c).Range.Text = strText;<br>}<br>oTable.Columns[1].Width = oWord.InchesToPoints(2); //Change width of columns 1 & 2<br>oTable.Columns[2].Width = oWord.InchesToPoints(3);<br><br>//Keep inserting text. When you get to 7 inches from top of the<br>//document, insert a hard page break.<br>object oPos;<br>double dPos = oWord.InchesToPoints(7);<br>oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.InsertParagraphAfter();<br>do<br>{<br>wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>wrdRng.ParagraphFormat.SpaceAfter = 6;<br>wrdRng.InsertAfter("A line of text");<br>wrdRng.InsertParagraphAfter();<br>oPos = wrdRng.get_Information<br> (Word.WdInformation.wdVerticalPositionRelativeToPage);<br>}<br>while(dPos >= Convert.ToDouble(oPos));<br>object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;<br>object oPageBreak = Word.WdBreakType.wdPageBreak;<br>wrdRng.Collapse(ref oCollapseEnd);<br>wrdRng.InsertBreak(ref oPageBreak);<br>wrdRng.Collapse(ref oCollapseEnd);<br>wrdRng.InsertAfter("We're now on page 2. Here's my chart:");<br>wrdRng.InsertParagraphAfter();<br><br>//添加一个chart<br><br>Word.InlineShape oShape;<br>object oClassType = "MSGraph.Chart.8";<br>wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>oShape = wrdRng.InlineShapes.AddOLEObject(ref oClassType, ref oMissing, <br>ref oMissing, ref oMissing, ref oMissing,<br>ref oMissing, ref oMissing, ref oMissing);<br><br>//Demonstrate use of late bound oChart and oChartApp objects to<br>//manipulate the chart object with MSGraph.<br>object oChart;<br>object oChartApp;<br>oChart = oShape.OLEFormat.Object;<br>oChartApp = oChart.GetType().InvokeMember("Application",<br>BindingFlags.GetProperty, null, oChart, null);<br><br>//Change the chart type to Line.<br>object[] Parameters = new Object[1];<br>Parameters[0] = 4; //xlLine = 4<br>oChart.GetType().InvokeMember("ChartType", BindingFlags.SetProperty,<br>null, oChart, Parameters);<br><br>//Update the chart image and quit MSGraph.<br>oChartApp.GetType().InvokeMember("Update",<br>BindingFlags.InvokeMethod, null, oChartApp, null);<br>oChartApp.GetType().InvokeMember("Quit",<br>BindingFlags.InvokeMethod, null, oChartApp, null);<br>//... If desired, you can proceed from here using the Microsoft Graph <br>//Object model on the oChart and oChartApp objects to make additional<br>//changes to the chart.<br><br>//Set the width of the chart.<br>oShape.Width = oWord.InchesToPoints(6.25f);<br>oShape.Height = oWord.InchesToPoints(3.57f);<br><br>//Add text after the chart.<br>wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;<br>wrdRng.InsertParagraphAfter();<br>wrdRng.InsertAfter("THE END.");<br><br>//Close this form.<br>this.Close();<br>}<br><br><br><br>使用模板<br>如果您要使用自动化功能创建的文档都是通用格式,则利用基于预设格式的模板的新文档来开始创建过程会更加容易。与从头创建文档相比,将某个模板与 Word 自动化客户端配合使用有两大优点: ? 您可以对整个文档中的对象的格式设置和布局施加更多控制。 <br>? 可以使用较少的代码创建文档。 <br>通过使用模板,可以精确地调整表格、段落和其他对象在文档中的布局,并可为这些对象添加格式设置。通过使用自动化功能,可以基于包含下面这样的代码的模板创建新文档: 在模板中,可以定义书签,这样,自动化客户端就可以在文档的特定位置加入可变文本,如下所示: 使用模板的另一个优点在于,您可以创建和存储希望在运行时应用的格式样式,如下所示: - 或者 - <br><br><br>object oTemplate = "c:\\MyTemplate.dot";<br>oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,<br> ref oMissing, ref oMissing);<br> <br><br>object oBookMark = "MyBookmark";<br>oDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";<br> <br><br>object oStyleName = "MyStyle";<br>oDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);<br> <br><br>object oStyleName = "MyStyle";<br><br> <br>
ThinkPHP是一种开源的PHP开发框架,它提供了丰富的功能和工具,可以用来快速高效地开发Web应用程序。在ThinkPHP框架中,我们可以使用第三方库或扩展来生成Word文档。 其中,PHPWord是一个流行的第三方库,可以通过它来生成Word文档。我们可以在ThinkPHP框架中使用Composer安装PHPWord库,然后通过加载和实例化类来创建Word文档。 下面是一个基本的示例代码,展示了在ThinkPHP框架中如何生成Word文档: 1. 首先,通过在项目根目录中的composer.json文件中添加PHPWord依赖项,并运行composer install安装库: ``` { "require": { "phpoffice/phpword": "0.17.*" } } ``` 2. 在ThinkPHP中的某个控制器方法中,引入PHPWord库并使用它来生成Word文档: ``` use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\IOFactory; public function generateWord() { // 实例化PhpWord对象 $phpWord = new PhpWord(); // 创建一个新的节 $section = $phpWord->addSection(); // 添加文本到节中 $section->addText('Hello, World!'); // 保存Word文档 $filePath = '/path/to/save/document.docx'; $phpWord->save($filePath); // 返回生成的文档路径等信息 return [ 'file_path' => $filePath, 'file_size' => filesize($filePath) ]; } ``` 以上代码中,我们首先实例化了一个PhpWord对象,然后创建了一个节和添加了文本内容。最后,通过调用save方法保存Word文档,并返回生成的文档路径和大小等信息。 通过这样的方式,我们可以在ThinkPHP框架中快速生成Word文档,并按需修改和扩展生成文档的内容和格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值