不准别人网站做反链,南京专业网站设计哪个品牌,公众号平台怎么弄的,怎么给网站做跳转以村庄规划制图为例#xff0c;通过对现状和规划用地的统计#xff0c;生成Excel格式的【空间功能结构调整表】后#xff0c;需要进一步将表格导出成图片#xff0c;并嵌入到图集中#xff0c;这样可以实现全流程不用手动参与#xff0c;让制图的流程完全自动化。
关于E…以村庄规划制图为例通过对现状和规划用地的统计生成Excel格式的【空间功能结构调整表】后需要进一步将表格导出成图片并嵌入到图集中这样可以实现全流程不用手动参与让制图的流程完全自动化。
关于Excel截图的方法从python、c#都曾经历过虽然有些方法已经不再用但还是记录下来。
Excel表格如下 导出的图片如下 基本是完美的截图。 1、Python的截图方法
python里处理Excel的库不少我使用的是PyWin32。
其它的库可能也能实现这个功能但我没找到。
直接上代码
import win32com.client as win32
from PIL import ImageGrab
import osexcel win32.Dispatch(Excel.Application)
wb excel.Workbooks.Open(excel_file)
ws wb.WorkSheets(excel_tb) # 打开工作簿# 示例截图的起始终止格
start_cell B2
end_cell G8ws.Range(f{start_cell}:{end_cell}).CopyPicture() # 变成图片
ws.Paste(ws.Range(start_cell)) # 将图片黏贴在excel中ws.Shapes(ws.Shapes.Count).Copy() # 图片至剪贴板
img ImageGrab.grabclipboard() # 从剪贴板获取图片
img.save(output_png_path) # 图片保存
wb.Save() # excel保存
wb.Close()
这里的方法有曲折相当于选定范围后在Excel里CTRLCCTRLV然后再将粘贴后的图片复制出去。
2、C#的截图方法一【Microsoft.Office.Interop.Excel】
【Microsoft.Office.Interop.Excel】库的功能非常强大但它需要安装微软的Office才能使用并且有兼容性和进程占用的问题缺点也很明显。实际上这个方法之前也写过这里复述一遍。
using Microsoft.Office.Interop.Excel;
using Application Microsoft.Office.Interop.Excel.Application;
using Range Microsoft.Office.Interop.Excel.Range;// Excel指定范围导出JPG图片
public static void ExcelImportToJPG(string excelPath, string startRange, string endRange, string outputPath)
{// 在UI线程上执行添加item的操作System.Windows.Application.Current.Dispatcher.Invoke(() {// 例如A1G6string rangeAddress startRange : endRange;// 创建Excel应用程序对象Application excelApp new Application();// 打开Excel文件Workbook workbook excelApp.Workbooks.Open(excelPath);// 获取工作表Worksheet worksheet workbook.Sheets[1];// 获取范围对象Microsoft.Office.Interop.Excel.Range range worksheet.Range[rangeAddress];// 获取范围的像素宽度和高度int widthInPixels (int)Math.Round(range.Width * 1.3333);int heightInPixels (int)Math.Round(range.Height * 1.3333);// 创建位图对象Bitmap bitmap new Bitmap(widthInPixels, heightInPixels);// 将范围内容复制到剪贴板range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);// 获取剪贴板图像数据System.Drawing.Image clipboardImage null;if (System.Windows.Forms.Clipboard.ContainsImage()){clipboardImage System.Windows.Forms.Clipboard.GetImage();// 在位图上绘制剪贴板图像using (Graphics graphics Graphics.FromImage(bitmap)){graphics.DrawImage(clipboardImage, 0, 0);}// 将位图保存为图片文件bitmap.Save(outputPath, ImageFormat.Jpeg); // 或者保存为PNG图片将第二个参数改为ImageFormat.Png}// 关闭和释放资源workbook.Close(false);excelApp.Quit();System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);// 清空剪贴板数据System.Windows.Forms.Clipboard.Clear();});
}
3、C#的截图方法二【Aspose.cells】
考虑到【Microsoft.Office.Interop.Excel】库的一些问题实在无法解决后来选择了【Aspose.cells】库作为替代那这个截图方法也得解决。
经网友【俊】的帮助【Aspose.cells】同样实现了这一方法完美替代了【Microsoft.Office.Interop.Excel】。
代码如下
using Aspose.Cells;
using Aspose.Cells.Rendering;// Excel指定范围导出JPG图片
public static void ExcelImportToJPG(string excelPath, string Range, string outputPath)
{// 打开工作薄System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);LoadOptions loadOptinos new LoadOptions(LoadFormat.Xlsx);Workbook wb new Workbook(excelPath, loadOptinos);// 打开工作表Worksheet sheet wb.Worksheets[0];// 定义要截图的单元格范围 例Range B2:G26Range range sheet.Cells.CreateRange(Range);// 设置打印属性ImageOrPrintOptions imgOptions new ImageOrPrintOptions();// 在一页内打印imgOptions.OnePagePerSheet true;// 只打印区域内imgOptions.OnlyArea true;// 打印SheetRender render new SheetRender(sheet, imgOptions);render.ToImage(0, outputPath);// 保存wb.Save(excelPath);wb.Dispose();
}