如何使用 Excel 对象将 DataGridView 数据导出到 Excel
2021-05-04 18:26
标签:ring try 帮助 vps pack 客户 filename 论坛 form 转载出处:https://code.msdn.microsoft.com/How-to-insert-image-into-93964561 本示例演示如何使用 Open XML SDK 将图像自动插入到电子表格中。一些客户经常在 MSDN 论坛上咨询此问题,但是 MSDN 上并不存在示例。因此,如果 MSDN 中存在此示例,客户将可以从此示例中获取帮助。 客户证明: http://social.msdn.microsoft.com/Forums/zh-CN/oxmlsdk/thread/4c369c2f-72ed-4e86-9c62-ff606d29ace2 http://social.msdn.microsoft.com/Forums/zh-CN/oxmlsdk/thread/5c60076e-9884-4298-a443-c97d941cf09d http://stackoverflow.com/questions/5793950/c-sharp-openxml-insert-an-image-into-an-excel-document 在 Visual Studio 2013 中打开项目 (InsertImageIntoExcel.csproj) 并生成项目。 从项目中复制出来的代码: Program.cs文件中的代码 Utility.cs中的代码 如何使用 Excel 对象将 DataGridView 数据导出到 Excel 标签:ring try 帮助 vps pack 客户 filename 论坛 form 原文地址:http://www.cnblogs.com/xiehaofeng/p/7701083.html本项目阐述如何使用 Open XML SDK 将图像插入到 Excel 中
简介
生成项目
class Utility
{
public static void CreatePackage(string sFile, string imageFileName)
{
try
{
// Create a spreadsheet document by supplying the filepath.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(sFile, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart
运行示例
//****************************** Module Header ******************************//Module Name: Program.cs
//Project: InsertImageIntoExcel
//Copyright (c) Microsoft Corporation
//The project illustrates how to insert image into Excel using Open XML SDK
//This source is subject to the Microsoft Public License.
//See http://www.microsoft.com/en-us/openness/resources/licenses.aspx#MPL.
//All other rights reserved.
//*****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Drawing;
namespace InsertImageIntoExcel
{
class Program
{
static void Main(string[] args)
{
try
{
string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string sFile = appPath + "\\InsertImage.xlsx";
string imageFile = appPath + "\\SampleImage.jpg";
// If the file exists, delete it
if (File.Exists(sFile))
{
File.Delete(sFile);
}
Utility.CreatePackage(sFile, imageFile);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
//****************************** Module Header ******************************//Module Name: Utility.cs
//Project: InsertImageIntoExcel
//Copyright (c) Microsoft Corporation
//The project illustrates how to insert image into Excel using Open XML SDK
//This source is subject to the Microsoft Public License.
//See http://www.microsoft.com/en-us/openness/resources/licenses.aspx#MPL.
//All other rights reserved.
//*****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using DocumentFormat.OpenXml.Drawing.Spreadsheet;
using Xdr = DocumentFormat.OpenXml.Drawing.Spreadsheet;
using A = DocumentFormat.OpenXml.Drawing;
using System.Drawing;
namespace InsertImageIntoExcel
{
class Utility
{
public static void CreatePackage(string sFile, string imageFileName)
{
try
{
// Create a spreadsheet document by supplying the filepath.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(sFile, SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart
文章标题:如何使用 Excel 对象将 DataGridView 数据导出到 Excel
文章链接:http://soscw.com/index.php/essay/82376.html