本文整理汇总了C#中Microsoft.Office.Interop.Excel.Application.InchesToPoints方法的典型用法代码示例。如果您正苦于以下问题:C# Application.InchesToPoints方法的具体用法?C# Application.InchesToPoints怎么用?C# Application.InchesToPoints使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Office.Interop.Excel.Application
的用法示例。
在下文中一共展示了Application.InchesToPoints方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveExcel
/// <summary>
/// 重新另存為Excel的xls
/// </summary>
/// <param name="path">保存路徑</param>
/// <returns></returns>
private bool SaveExcel(string path)
{
bool result = false;
Excel.Application AppExcel = new Excel.Application();
try
{
if (AppExcel == null)
{
throw new Exception("无法创建Excel对象,可能您的机器未安装Excel");
}
Excel.Workbook workBook = AppExcel.Application.Workbooks.Open(path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];
Excel.Range workRange = workSheet.Cells[2, 1] as Excel.Range;//工序
workRange.ColumnWidth = 15;
workRange = workSheet.Cells[2, 2] as Excel.Range;//機種
workRange.ColumnWidth = 20;
workRange = workSheet.Cells[2, 3] as Excel.Range;//機台位置
workRange.ColumnWidth = 20;
workRange = workSheet.Cells[2, 4] as Excel.Range;//機台編號
workRange.ColumnWidth = 15;
workRange = workSheet.Cells[2, 5] as Excel.Range;//班次內日期
workRange.ColumnWidth = 20;
workRange = workSheet.Cells[2, 6] as Excel.Range;//板數
workRange.ColumnWidth = 10;
workRange = workSheet.Cells[2, 7] as Excel.Range;//產量
workRange.ColumnWidth = 15;
workSheet.Name = this.m_strExportExcelName + DateTime.Now.ToString("yyyy-MM-dd");
workSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4; //設置頁面A4打印
workSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlPortrait; //設置橫向打印
workSheet.PageSetup.Zoom = false; //設置頁面縮放比例 Zoom必須設為False FitToPagesWide才有效
workSheet.PageSetup.FitToPagesWide = 1;//設置葉寬為一頁
workSheet.PageSetup.CenterHorizontally = true;//頁面水平居中
workSheet.PageSetup.TopMargin = AppExcel.InchesToPoints(0.25);
workSheet.PageSetup.BottomMargin = AppExcel.InchesToPoints(0.2);
workSheet.PageSetup.LeftMargin = AppExcel.InchesToPoints(0.2);
workSheet.PageSetup.RightMargin = AppExcel.InchesToPoints(0.2);
workSheet.PageSetup.FooterMargin = AppExcel.InchesToPoints(0.3);
workSheet.PageSetup.HeaderMargin = AppExcel.InchesToPoints(0.3);
this.Invoke(new InvokeSetProgressVal(SetProgressBarVal), new object[] { 90 });
AppExcel.Visible = false;
workBook.SaveCopyAs(this.m_strSaveExcelPath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
AppExcel.Quit();
IntPtr t = new IntPtr(AppExcel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
System.GC.Collect();
}
return result;
}
示例2: SaveExcel
//重新另存為Excel的xls
private bool SaveExcel(string path)
{
bool result = false;
Excel.Application AppExcel = new Excel.Application();
try
{
if (AppExcel == null)
{
throw new Exception("无法创建Excel对象,可能您的机器未安装Excel");
}
Excel.Workbook workBook = AppExcel.Application.Workbooks.Open(path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];
workSheet.Name = "過膠機生產日報表" + DateTime.Now.ToString("yyyy-MM-dd");
workSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4; //設置頁面A4打印
workSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape; //設置橫向打印
workSheet.PageSetup.Zoom = false; //設置頁面縮放比例 Zoom必須設為False FitToPagesWide才有效
workSheet.PageSetup.FitToPagesWide = 1;//設置葉寬為一頁
workSheet.PageSetup.CenterHorizontally = true;//頁面水平居中
workSheet.PageSetup.TopMargin = AppExcel.InchesToPoints(0.275590551181102);
workSheet.PageSetup.BottomMargin = AppExcel.InchesToPoints(0.196850393700787);
workSheet.PageSetup.LeftMargin = AppExcel.InchesToPoints(0.196850393700787);
workSheet.PageSetup.RightMargin = AppExcel.InchesToPoints(0.196850393700787);
workSheet.PageSetup.FooterMargin = AppExcel.InchesToPoints(0.31496062992126);
workSheet.PageSetup.HeaderMargin = AppExcel.InchesToPoints(0.31496062992126);
AppExcel.get_Range(AppExcel.Cells[4, 15], AppExcel.Cells[m_dataTabel.Rows.Count + 3, 17]).NumberFormat = "0.00";
AppExcel.get_Range(AppExcel.Cells[4, 27], AppExcel.Cells[m_dataTabel.Rows.Count + 3, 27]).NumberFormat = "0.00";
AppExcel.Visible = false;
workBook.SaveCopyAs(m_SaveExcelPath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//if (File.Exists(m_TempPath)) File.Delete(m_TempPath); //刪除開始生成的臨時xls
AppExcel.Quit();
IntPtr t = new IntPtr(AppExcel.Hwnd); //杀死进程的好方法
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
System.GC.Collect();
}
return result;
}
示例3: DataGridviewShowToExcel
public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle)
{
if (dgv.Rows.Count == 0)
{
MessageBox.Show("当前没有数据,导出失败!");
return false;
}
//建立Excel对象
//int columnCount = dgv.Columns.Count;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
Microsoft.Office.Interop.Excel._Workbook workBook = excel.Workbooks.Add(true);
//新建工作表
Microsoft.Office.Interop.Excel._Worksheet worksheet = workBook.ActiveSheet as Microsoft.Office.Interop.Excel._Worksheet;
worksheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait;//横向打印
worksheet.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA3;
worksheet.PageSetup.CenterHorizontally = true;//水平垂直
//worksheet.PageSetup.CenterVertically = true;//
Microsoft.Office.Interop.Excel.Range titleRange = worksheet.get_Range(
worksheet.Cells[1, 1], worksheet.Cells[1, 5]); //选取单元格
titleRange.Merge(true);//合并单元格
titleRange.Value2 = "已使用标识卡统计查询表";//设置单元格内容
titleRange.Font.Name = "黑体";//设置字体
titleRange.Font.Size = 15;//设置字体大小
titleRange.Font.Bold = true;//字体加粗
titleRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;//水平居中
titleRange.VerticalAlignment = XlVAlign.xlVAlignCenter;//垂直居中
double top = 0;
double left = 0;
worksheet.PageSetup.TopMargin = excel.InchesToPoints(top / 1);
worksheet.PageSetup.LeftMargin = excel.InchesToPoints(left / 0);
Microsoft.Office.Interop.Excel.Range titleRange2 = worksheet.get_Range(
worksheet.Cells[2, 1], worksheet.Cells[2, 5]); //选取单元格
titleRange2.Merge(true);//合并单元格
titleRange2.Value2 = dtpBegin.Text+"至"+dtpEnd.Text+" "+"统计人:";//设置单元格内容
//titleRange.Font.Name = "黑体";//设置字体
titleRange2.Font.Size = 10;//设置字体大小
titleRange2.Font.Bold = true;//字体加粗
titleRange2.HorizontalAlignment = XlHAlign.xlHAlignLeft;//水平居中
titleRange2.VerticalAlignment = XlVAlign.xlVAlignCenter;//垂直居中
//设置单元格宽度和高度
excel.get_Range("A1", Missing.Value).ColumnWidth = 15;
excel.get_Range("B1", Missing.Value).ColumnWidth = 15;
excel.get_Range("C1", Missing.Value).ColumnWidth = 15;
excel.get_Range("D1", Missing.Value).ColumnWidth = 15;
excel.get_Range("E1", Missing.Value).ColumnWidth = 15;
excel.get_Range("A1", Missing.Value).RowHeight = 30;
excel.get_Range("A2", Missing.Value).RowHeight = 30;
excel.get_Range("A3", Missing.Value).RowHeight = 30;
excel.get_Range("A4", Missing.Value).RowHeight = 30;
excel.get_Range("A5", Missing.Value).RowHeight = 30;
excel.get_Range("A6", Missing.Value).RowHeight = 30;
excel.get_Range("A7", Missing.Value).RowHeight = 30;
//生成字段名称
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[3, i + 1] = dgv.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < dgv.RowCount - 1; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 4, j + 1] = dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 4, j + 1] = dgv[j, i].Value.ToString();
}
//设置边框
excel.get_Range(excel.Cells[dgv.Rows.Count + 2, 5], excel.Cells[3, dgv.Rows.Count]).Cells.Borders.LineStyle = 1;
excel.get_Range(excel.Cells[dgv.Rows.Count + 2, 5], excel.Cells[3, dgv.Rows.Count]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
excel.get_Range(excel.Cells[dgv.Rows.Count+2, 4], excel.Cells[3, dgv.Rows.Count]).Cells.Borders.LineStyle = 1;
excel.get_Range(excel.Cells[dgv.Rows.Count+2, 4], excel.Cells[3, dgv.Rows.Count]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
excel.get_Range(excel.Cells[dgv.Rows.Count+2, 3], excel.Cells[3, dgv.Rows.Count]).Cells.Borders.LineStyle = 1;
excel.get_Range(excel.Cells[dgv.Rows.Count+2, 3], excel.Cells[3, dgv.Rows.Count]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
excel.get_Range(excel.Cells[dgv.Rows.Count+2, 2], excel.Cells[3, dgv.Rows.Count]).Cells.Borders.LineStyle = 1;
excel.get_Range(excel.Cells[dgv.Rows.Count+2, 2], excel.Cells[3, dgv.Rows.Count]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
excel.get_Range(excel.Cells[dgv.Rows.Count+2, 1], excel.Cells[3, dgv.Rows.Count]).Cells.Borders.LineStyle = 1;
excel.get_Range(excel.Cells[dgv.Rows.Count+2, 1], excel.Cells[3, dgv.Rows.Count]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
}
}
return true;
//.........这里部分代码省略.........