用c#在excel中插入图片和设置表格宽度
2016-11-09 22:14
2671 查看
问题的由来是我想在excel中自动插入图片,插入图片后我想根据图片的大小调整cell的大小,于是不经意间就来到了一个坑的面前。。。。
Range对象有ColumnWidth属性和RowHeight属性,这两个属性的单位不同,并且都不是像素。。。
RowHeight的单位是point,可以这样换算
ColumnWidth的单位是在默认字体中,0-9这几个字符中最宽的宽度。这个值在C#中好像没有直接获取的方法,最后找了一个trick的办法
System.Drawing.Font font = new System.Drawing.Font("宋体", 11.0f, FontStyle.Regular);
int w0 = System.Windows.Forms.TextRenderer.MeasureText("____", font).Width;
int w = System.Windows.Forms.TextRenderer.MeasureText("__0__", font).Width;
digitWidth = w - w0;
好在宋体中数字的字符宽度都一样。另外注意直接用MeasureText("0", font).Width是不行的,中间会有padding。
故事到这里还没有结束。真正的pixel到ColumnWidth的计算公式是这样的:Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100
写出来就是这样
另外,想把图片保存到excel中有两个方法
方法1
Pictures pictures = worksheet.Pictures() as Pictures;
Picture pic = pictures.Insert(fileName);
这样保存的是图片的链接,图片文件的位置不能变
方法2会将文件内容保存到xls文件中
Microsoft.Office.Interop.Excel.Shape shape = worksheet .Shapes.AddPicture(fileName,
MsoTriState.msoFalse, MsoTriState.msoTrue, l, t, -1, -1);
先是用参数-1, -1将图片按原大小插入,然后再保持比例缩放
shape.Height = PixelToPointY(height);
最后的宽度还有有一点点对不上,不过问题不大。
参考
https://support.office.com/en-us/article/change-the-column-width-and-row-height-72f5e3cc-994d-43e8-ae58-9774a0905f46 https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.column.aspx http://stackoverflow.com/questions/7716078/formula-to-convert-net-pixels-to-excel-width-in-openxml-format
Range对象有ColumnWidth属性和RowHeight属性,这两个属性的单位不同,并且都不是像素。。。
RowHeight的单位是point,可以这样换算
public static int PixelToPointY(int pixels) { return (int)((((double)pixels) * 1440.0) / ((double)GetPPIVertical() * 20)); } public static int GetPPIVertical() { int ppi; IntPtr dc = GetDC(IntPtr.Zero); ppi = GetDeviceCaps(dc, 90); //DEVICECAP LOGPIXELSY ReleaseDC(IntPtr.Zero, dc); return ppi; }
ColumnWidth的单位是在默认字体中,0-9这几个字符中最宽的宽度。这个值在C#中好像没有直接获取的方法,最后找了一个trick的办法
System.Drawing.Font font = new System.Drawing.Font("宋体", 11.0f, FontStyle.Regular);
int w0 = System.Windows.Forms.TextRenderer.MeasureText("____", font).Width;
int w = System.Windows.Forms.TextRenderer.MeasureText("__0__", font).Width;
digitWidth = w - w0;
好在宋体中数字的字符宽度都一样。另外注意直接用MeasureText("0", font).Width是不行的,中间会有padding。
故事到这里还没有结束。真正的pixel到ColumnWidth的计算公式是这样的:Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100
写出来就是这样
public static float PixelsToColumnWdthX(int pixels) { return (float)((int)((float)(pixels - 5) / (float)digitWidth * 100f + 0.5f)) / 100f; }
另外,想把图片保存到excel中有两个方法
方法1
Pictures pictures = worksheet.Pictures() as Pictures;
Picture pic = pictures.Insert(fileName);
这样保存的是图片的链接,图片文件的位置不能变
方法2会将文件内容保存到xls文件中
Microsoft.Office.Interop.Excel.Shape shape = worksheet .Shapes.AddPicture(fileName,
MsoTriState.msoFalse, MsoTriState.msoTrue, l, t, -1, -1);
先是用参数-1, -1将图片按原大小插入,然后再保持比例缩放
shape.Height = PixelToPointY(height);
最后的宽度还有有一点点对不上,不过问题不大。
参考
https://support.office.com/en-us/article/change-the-column-width-and-row-height-72f5e3cc-994d-43e8-ae58-9774a0905f46 https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.column.aspx http://stackoverflow.com/questions/7716078/formula-to-convert-net-pixels-to-excel-width-in-openxml-format
相关文章推荐
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- C#中操作Excel(4)—— 向Excel中插入两种图表以及设置图表格式
- C#向Excel报表中插入图片的2种方法
- asp.net两种方式在模板excel中插入图片:npoi和c# com组件
- 【转载】C#向Excel报表中插入图片的2种方法
- C#向Excel报表中插入图片的2种方法
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- c#之Word基本操作----创建,打开,绘制表格,插入图片等
- C#向Excel报表中插入图片的2种方法
- c# 图片插入Excel
- 黄聪:C# web word文档操作,任意指定位置插入图片,表格简单操作
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
- C#向Excel报表中插入图片的2种方法
- C# Excel插入图形,添加文本,填充颜色和图片
- C#中设置EXCEL文件中表格边框格式
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
- ExtJS4.1.1 设置表格背景颜色 修改文本颜色 在表格中插入图片
- Excel:插入图片随表格隐藏,或显示
- C#向EXCEL中插入图表对象,并设置标题