MS OpenXML SDK 2.0在Excel中指定位置填入数据和插入图片

简介

这是公司另外一个项目组遇到的技术问题。即如何用OpenXML在Excel指定位置填入数据和插入图片。笔者知道此事之后,帮他们想了一下办法。

用户的需求是这样的:有一个Excel的文档模版,里面已经有了一个表格框架。需要在某些单元格填入数字或者文字。并在某些单元格插入图片。并且不破坏原有的表格内容。这些功能必须在asp.net应用程序中实现,并且不用Office interop的组件。他们之前已经用了Office interop对象来操作Excel,这样就要求服务器上安装Office,因为之前的方法占用内存大,不稳定,所以用户希望他们另外想办法。用户和他们都知道OpenXML可以读写Excel文件,而且很轻量的,占用资源小,但是他们不知道如何用OpenXML完成这个任务,卡壳好多天了。

需求的理解可以见上图,我们需要做的是:

1. 在黄色背景的单元格填入数据

2. 在指定位置插入图片(如上图中的折线图)

上图左图是未填入数据之前的样子。上图右图是完成后的最终结果图。

实现

他们在网路上找了一些参考资料和参考代码,但是其中的对象模型比较复杂,一时半会没有弄明白。笔者聆听他们的需求后,再看了他们的已有代码。调试了一阵,发现这些参考代码对实现他们的需求没有什么用。需要针对他们的需求来改代码才行。

此工程必须引用C:\Program Files (x86)\Open XML SDK\V2.0\lib\DocumentFormat.OpenXml.dll,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\WindowsBase.dll

以下是核心代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Drawing.Spreadsheet;
using System.Text.RegularExpressions;
using System.Data;
namespace ExcelWriter
{
    public class OpenXMLExcel : IDisposable
    {
        #region Constrution and dispose

        SpreadsheetDocument spreadSheet;
        public WorksheetPart CurrentWorksheetPart { get; set; }
        SharedStringTablePart shareStringPart;
        public OpenXMLExcel(Stream stream, bool bCreate)
        {
            if (bCreate == true)
            {
                spreadSheet = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);
                WorkbookPart workbookPart = spreadSheet.AddWorkbookPart();
                workbookPart.Workbook = new Workbook();

                WorksheetPart worksheetPart = InsertWorksheet(spreadSheet.WorkbookPart);

                if (spreadSheet.WorkbookPart.GetPartsOfType().Count() > 0)
                {
                    shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType().First();
                }
                else
                {
                    shareStringPart = spreadSheet.WorkbookPart.AddNewPart();
                }

                shareStringPart.SharedStringTable = new SharedStringTable();
                shareStringPart.SharedStringTable.Count = 1;
                shareStringPart.SharedStringTable.UniqueCount = 1;

                CurrentWorksheetPart = worksheetPart;
            }
            else
            {
                spreadSheet = SpreadsheetDocument.Open(stream, true);
                shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType().First();
                CurrentWorksheetPart = spreadSheet.WorkbookPart.WorksheetParts.First();
            }
        }
        public void Dispose()
        {
            spreadSheet.Close();
            spreadSheet.Dispose();
        }

        #endregion
        #region Public interface
        public void WriteData(int x, int y, string strContent)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Column1");
            dt.Rows.Add(strContent);
            WriteDataIntoWorkSheet(x, y, dt);
        }

        public void WriteDataIntoWorkSheet(int startx, int starty, DataTable dt)
        {
            //if (startx < 1)
            //    startx = 1;
            //if (starty < 1)
            //    starty = 1;

            WorksheetPart worksheetPart = CurrentWorksheetPart;
            //starty -= 1;
            int j = 0;
            foreach (DataRow dr in dt.Rows)
            {
                j++;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    string name = GetColumnName(i + starty);
                    string text = Convert.IsDBNull(dr[i]) ? "" : dr[i].ToString();
                    int index = InsertSharedStringItem(text, shareStringPart);
                    Cell cell = InsertCellInWorksheet(name, Convert.ToUInt32(j + startx), worksheetPart);

                    cell.CellValue = new CellValue(index.ToString());
                    cell.DataType = new EnumValue(CellValues.SharedString);
                    worksheetPart.Worksheet.Save();
                }
            }
        }
        public void WriteDataIntoWorkSheet(int startx, int starty, string[][] data)
        {
            WorksheetPart worksheetPart = CurrentWorksheetPart;
            starty -= 1;
            int i = 0;
            foreach (string[] row in data)
            {
                int j = 0;
                foreach (string text in row)
                {
                    string name = GetColumnName(j + starty);
                    int index = InsertSharedStringItem(text, shareStringPart);
                    Cell cell = InsertCellInWorksheet(name, Convert.ToUInt32(i + startx), worksheetPart);

                    cell.CellValue = new CellValue(index.ToString());
                    cell.DataType = new EnumValue(CellValues.SharedString);
                    worksheetPart.Worksheet.Save();
                    j++;
                }
                i++;
            }
        }
        public void AddNewWorksheet()
        {
            WorkbookPart workbookPart = spreadSheet.WorkbookPart;

            WorksheetPart newWorksheetPart = workbookPart.AddNewPart();
            newWorksheetPart.Worksheet = new Worksheet(new SheetData());
            newWorksheetPart.Worksheet.Save();
            CurrentWorksheetPart = newWorksheetPart;
            //workbookPart.SharedStringTablePart.SharedStringTable.Count = workbookPart.SharedStringTablePart.SharedStringTable.Count + 1;
            //workbookPart.SharedStringTablePart.SharedStringTable.UniqueCount = workbookPart.SharedStringTablePart.SharedStringTable.UniqueCount + 1;
            string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart);
            Sheets sheets = workbookPart.Workbook.GetFirstChild();
            uint sheetId = 1;
            if (sheets.Elements().Count() > 0)
            {
                sheetId = sheets.Elements().Select(s => s.SheetId.Value).Max() + 1;
            }

            string sheetName = "Sheet" + sheetId;

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
            sheets.Append(sheet);
            workbookPart.Workbook.Save();
        }

        public void InsertImage(long x, long y, long? width, long? height, string sImagePath)
        {
            try
            {
                WorksheetPart wsp = CurrentWorksheetPart;
                DrawingsPart dp;
                ImagePart imgp;
                WorksheetDrawing wsd;

                ImagePartType ipt;
                switch (sImagePath.Substring(sImagePath.LastIndexOf('.') + 1).ToLower())
                {
                    case "png":
                        ipt = ImagePartType.Png;
                        break;
                    case "jpg":
                    case "jpeg":
                        ipt = ImagePartType.Jpeg;
                        break;
                    case "gif":
                        ipt = ImagePartType.Gif;
                        break;
                    default:
                        return;
                }

                if (wsp.DrawingsPart == null)
                {
                    //----- no drawing part exists, add a new one

                    dp = wsp.AddNewPart();
                    imgp = dp.AddImagePart(ipt, wsp.GetIdOfPart(dp));
                    wsd = new WorksheetDrawing();
                }
                else
                {
                    //----- use existing drawing part

                    dp = wsp.DrawingsPart;
                    imgp = dp.AddImagePart(ipt);
                    dp.CreateRelationshipToPart(imgp);
                    wsd = dp.WorksheetDrawing;
                }

                using (FileStream fs = new FileStream(sImagePath, FileMode.Open))
                {
                    imgp.FeedData(fs);
                }

                int imageNumber = dp.ImageParts.Count();
                if (imageNumber == 1)
                {
                    Drawing drawing = new Drawing();
                    drawing.Id = dp.GetIdOfPart(imgp);
                    CurrentWorksheetPart.Worksheet.Append(drawing);
                }

                NonVisualDrawingProperties nvdp = new NonVisualDrawingProperties();
                nvdp.Id = new UInt32Value((uint)(1024 + imageNumber));
                nvdp.Name = "Picture " + imageNumber.ToString();
                nvdp.Description = "";
                DocumentFormat.OpenXml.Drawing.PictureLocks picLocks = new DocumentFormat.OpenXml.Drawing.PictureLocks();
                picLocks.NoChangeAspect = true;
                picLocks.NoChangeArrowheads = true;
                NonVisualPictureDrawingProperties nvpdp = new NonVisualPictureDrawingProperties();
                nvpdp.PictureLocks = picLocks;
                NonVisualPictureProperties nvpp = new NonVisualPictureProperties();
                nvpp.NonVisualDrawingProperties = nvdp;
                nvpp.NonVisualPictureDrawingProperties = nvpdp;

                DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
                stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle();

                BlipFill blipFill = new BlipFill();
                DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip();
                blip.Embed = dp.GetIdOfPart(imgp);
                blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print;
                blipFill.Blip = blip;
                blipFill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle();
                blipFill.Append(stretch);

                DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D();
                DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset();
                offset.X = 0;
                offset.Y = 0;
                t2d.Offset = offset;
                Bitmap bm = new Bitmap(sImagePath);

                DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();

                if (width == null)
                    extents.Cx = (long)bm.Width * (long)((float)914400 / bm.HorizontalResolution);
                else
                    extents.Cx = width * (long)((float)914400 / bm.HorizontalResolution);

                if (height == null)
                    extents.Cy = (long)bm.Height * (long)((float)914400 / bm.VerticalResolution);
                else
                    extents.Cy = height * (long)((float)914400 / bm.VerticalResolution);

                bm.Dispose();
                t2d.Extents = extents;
                ShapeProperties sp = new ShapeProperties();
                sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto;
                sp.Transform2D = t2d;
                DocumentFormat.OpenXml.Drawing.PresetGeometry prstGeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry();
                prstGeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle;
                prstGeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList();
                sp.Append(prstGeom);
                sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill());

                DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture();
                picture.NonVisualPictureProperties = nvpp;
                picture.BlipFill = blipFill;
                picture.ShapeProperties = sp;

                Position pos = new Position();
                pos.X = x * 914400 / 72;
                pos.Y = y * 914400 / 72;
                Extent ext = new Extent();
                ext.Cx = extents.Cx;
                ext.Cy = extents.Cy;
                AbsoluteAnchor anchor = new AbsoluteAnchor();
                anchor.Position = pos;
                anchor.Extent = ext;
                anchor.Append(picture);
                anchor.Append(new ClientData());
                wsd.Append(anchor);
                wsd.Save(dp);
            }
            catch (Exception ex)
            {
                throw ex; // or do something more interesting if you want
            }
        }

        public void InsertImage(long x, long y, string sImagePath)
        {
            InsertImage(x, y, null, null, sImagePath);
        }

        #endregion
        #region private static OpenXml methods

        private static int InsertSharedStringItem(string text, SharedStringTablePart shareStringPart)
        {
            // If the part does not contain a SharedStringTable, create one.
            if (shareStringPart.SharedStringTable == null)
            {
                shareStringPart.SharedStringTable = new SharedStringTable();
                shareStringPart.SharedStringTable.Count = 1;
                shareStringPart.SharedStringTable.UniqueCount = 1;
            }
            int i = 0;
            // Iterate through all the items in the SharedStringTable. If the text already exists, return its index.
            foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements())
            {
                if (item.InnerText == text)
                {
                    return i;
                }
                i++;
            }

            // The text does not exist in the part. Create the SharedStringItem and return its index.
            shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));
            shareStringPart.SharedStringTable.Save();

            return i;
        }

        private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
        {
            Worksheet worksheet = worksheetPart.Worksheet;
            SheetData sheetData = worksheet.GetFirstChild();
            string cellReference = columnName + rowIndex;

            // If the worksheet does not contain a row with the specified row index, insert one.
            Row row;
            if (sheetData.Elements().Where(r => r.RowIndex == rowIndex).Count() != 0)
            {
                row = sheetData.Elements().Where(r => r.RowIndex == rowIndex).First();
            }
            else
            {
                row = new Row() { RowIndex = rowIndex };
                sheetData.Append(row);
            }

            // If there is not a cell with the specified column name, insert one.
            if (row.Elements().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
            {
                return row.Elements().Where(c => c.CellReference.Value == cellReference).First();
            }
            else
            {
                // Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
                Cell refCell = null;
                foreach (Cell cell in row.Elements())
                {
                    if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
                    {
                        refCell = cell;
                        break;
                    }
                }

                Cell newCell = new Cell() { CellReference = cellReference };
                row.InsertBefore(newCell, refCell);

                worksheet.Save();
                return newCell;
            }
        }

        private static WorksheetPart InsertWorksheet(WorkbookPart workbookPart)
        {
            // Add a new worksheet part to the workbook.
            WorksheetPart newWorksheetPart = workbookPart.AddNewPart();
            newWorksheetPart.Worksheet = new Worksheet(new SheetData());
            newWorksheetPart.Worksheet.Save();

            workbookPart.Workbook.AppendChild(new Sheets());

            Sheets sheets = workbookPart.Workbook.GetFirstChild();
            string relationshipId = workbookPart.GetIdOfPart(newWorksheetPart);

            // Get a unique ID for the new sheet.
            uint sheetId = 1;
            if (sheets.Elements().Count() > 0)
            {
                sheetId = sheets.Elements().Select(s => s.SheetId.Value).Max() + 1;
            }

            string sheetName = "Sheet" + sheetId;

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
            sheets.Append(sheet);

            workbookPart.Workbook.Save();
            return newWorksheetPart;
        }

        // Given a Worksheet and a cell name, verifies that the specified cell exists.
        // If it does not exist, creates a new cell.
        private static void CreateSpreadsheetCellIfNotExist(Worksheet worksheet, string cellName)
        {
            string columnName = GetColumnName(cellName);
            uint rowIndex = GetRowIndex(cellName);

            IEnumerable rows = worksheet.Descendants().Where(r => r.RowIndex.Value == rowIndex);

            // If the Worksheet does not contain the specified row, create the specified row.
            // Create the specified cell in that row, and insert the row into the Worksheet.
            if (rows.Count() == 0)
            {
                Row row = new Row() { RowIndex = new UInt32Value(rowIndex) };
                Cell cell = new Cell() { CellReference = new StringValue(cellName) };
                row.Append(cell);
                worksheet.Descendants().First().Append(row);
                worksheet.Save();
            }
            else
            {
                Row row = rows.First();

                IEnumerable cells = row.Elements().Where(c => c.CellReference.Value == cellName);

                // If the row does not contain the specified cell, create the specified cell.
                if (cells.Count() == 0)
                {
                    Cell cell = new Cell() { CellReference = new StringValue(cellName) };
                    row.Append(cell);
                    worksheet.Save();
                }
            }

        }
        // Given a cell name, parses the specified cell to get the column name.
        private static string GetColumnName(string cellName)
        {
            // Create a regular expression to match the column name portion of the cell name.
            Regex regex = new Regex("[A-Za-z]+");
            Match match = regex.Match(cellName);

            return match.Value;
        }

        // Given a cell name, parses the specified cell to get the row index.
        private static uint GetRowIndex(string cellName)
        {
            // Create a regular expression to match the row index portion the cell name.
            Regex regex = new Regex(@"\d+");
            Match match = regex.Match(cellName);

            return uint.Parse(match.Value);
        }

        #endregion
        #region Utility methods
        private static string GetColumnName(int index)
        {
            string name = "";
            char[] columnNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            int num = index;
            do
            {
                int i = num % 26;
                name = columnNames[i] + name;
                num = num / 26 - 1;
            } while (num > -1);
            if (string.IsNullOrEmpty(name))
                name = "A";
            return name;
        }
        #endregion
    }

}

 

要使用上述代码,就可以用如下的一段代码:

FileStream file = null;
            OpenXMLExcel openXMLExcel = null;
            try
            {
                file = new FileStream(@"C:\book1.xlsx", FileMode.Open);
                openXMLExcel = new OpenXMLExcel(file, false);
                openXMLExcel.WriteData(1, 1, "数学");
                openXMLExcel.WriteData(3, 1, "物理");
                openXMLExcel.InsertImage(1, 70, @"c:\TempPic.jpg");
                openXMLExcel.Dispose();
                file.Close();
            }
            catch
            {
                if (openXMLExcel != null)
                {
                    openXMLExcel.Dispose();
                }
                if (file != null)
                {
                    file.Close();
                }
            }
            finally
            {
            }

 

FileMode.Open是为了读入已有的Excel文件的字节流。

new OpenXMLExcel(file, false)中的false是为了进行读取操作, 此参数如果为true的话,就会建立一个新的Excel Sheet。

openXMLExcel.WriteData(1, 1, "数学"); 是为了将“数学”两字写入黄色背景的单元格,前一个1是横向坐标参数,表示第1行,注意这是以单元格为单位的坐标,这个行号从0开始。后一个1是纵向组表参数,表示第一列,注意这个列号也是从0开始。即最左上的单元格的坐标是(0,0)。

openXMLExcel.InsertImage(1, 70, @"c:\TempPic.jpg"); 是为了将c:\TempPic.jpg插入到坐标为1, 70的坐标处。注意这是以象素Pixel为单位的坐标。坐标原点在左上角。

OpenXML局限性

此OpenXML不支持Excel 2003或者更早的文件格式。只支持Excel 2007及Excel 2010的文件格式。可能是因为从Excel 2007起,这些文件都用xml表示了。xlsx其实是一个zip文件,解开此zip文件,我们可以看到很多描述excel内容的xml文件。

Enjoy coding life!

时间: 2024-10-25 12:57:21

MS OpenXML SDK 2.0在Excel中指定位置填入数据和插入图片的相关文章

更新字符串列表中,指定位置的字符串

字符串 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_updatestr]') and xtype in (N'FN', N'IF', N'TF'))drop function [dbo].[f_updatestr]GO /*--更新字符串列表中,指定位置的字符串  更新字符串列表中,指定位置的字符串为新的值 如果位置超出范围,则不做更新 --邹建 2004.07--*/ /*--调用示例  sel

删除字符串中指定位置的字符

/********************************************************************** * 版权所有 (C)2015, Wu Yingqiang. * * 文件名称:DelPosChar.c * 文件标识:无 * 内容摘要:删除字符串中指定位置的字符 * 其它说明:无 * 当前版本: V1.0 * 作 者: Wu Yingqiang * 完成日期: 20150115 * ***********************************

c语言-C替换文件中指定位置处的内容

问题描述 C替换文件中指定位置处的内容 a.txt中的内容:key1=111key2=222key3=333 现想把key2的值替换成666如何直接找到那个位置把值给替换了 解决方案 读取文件内容到字符串,然后找到字符串位置,修改对应的字符.然后这个字符串写回文件 解决方案二: C++替换文件中指定的内容 解决方案三: static void Main(string[] args) { string s = "key1=111key2=222key3=333"; Console.Wri

asp.net remove 删除字符串中指定位置的字符串片

 代码如下 复制代码 string str = "0123456789";           Console.WriteLine(str.Remove(5));   Console.WriteLine(str.Remove(5,1)); 实例 Remove() 从指定位置开始删除指定数的字符  代码如下 复制代码 <% string str1="我是变形金刚的崇拜者之一"; Response.Write(str1.Remove(6,4)); //结果为&qu

环信3.0,Android中,发送方发送一张本地图片,退出再进来,图片路径不正确。

问题描述 我发送一张本地图片后,用imgBody.getLocalUrl()这个方法去看这个图片的路径,是正确的,在sd卡中,显示也正常.等我退出之后,在进来,这个图片就不能正常的显示了,在查看路径就不对了,为null,这个怎么解决? 解决方案 再次进来看看获取到的localurl下有没有对应的文件存在

C#获取Excel中的数据到DataTable

例1  代码如下 复制代码 #region 获取Excel中指定工作薄的数据到DataTable /// <SUMMARY> /// 获取Excel中指定工作薄的数据到DataTable /// </SUMMARY> /// <PARAM name="excelPath" />Excel路径 /// <PARAM name="sheetName" />工作薄名称 /// <RETURNS>返回结果为Data

VBA在Excel中的应用(三)

目录 Chart Export Chart Format Chart Lengend Chart Protect Chart Title Chart Chart Export 1.将Excel中的图表导出成gif格式的图片保存到硬盘上 Sub ExportChart() Dim myChart As Chart Set myChart=ActiveChart myChart.Export Filename:="C:\Chart.gif", Filtername:="GIF&q

在excel中输入欧姆符号方法

  如何在word中打出欧姆符号? 1."插入"-"符号"里找找,如果你不想找那么麻烦,更快捷的办法是在"字符代码"(也在"插入"-"符号"里)处输入"03a9"就直接可看到了. 2."插入"-"符号"--子集--基本希腊语--就可以找到了. 3."插入"-对象-公式,即用公式编辑器可以输入. 如何在EXCEL中找到欧姆符号&q

页面&amp;amp;lt;img&amp;amp;gt;标签显示的图片,请问用java可以导入到excel中么?怎么导入?

问题描述 页面<img>标签显示的图片,请问用java可以导入到excel中么?怎么导入? oracle存储图片的类型是blob,这个是可以在页面读取出来的,但是怎么导入到excel中呢? 解决方案 img需要根据src用httpclient先下载,然后再插入图片到excel中. 解决方案二: 如果是本地的图片,并且blob类型,存储的,需要导出到文件. 解决方案三: http://stackoverflow.com/questions/681206/insert-image-into-exc