Interop 修改已存在的EXCEL 新增一列
来源:互联网 发布:淘宝店铺严重违规48分 编辑:程序博客网 时间:2024/06/11 23:18
using EXCELAPP = Microsoft.Office.Interop.Excel;<span style="white-space:pre"></span>#region Modify Existing Excel File //file name string filename = @"C:/xxxx.xlsx"; EXCELAPP.Application app = new EXCELAPP.Application(); app.DisplayAlerts = false; app.Visible = false; //EXCELAPP.Workbook workbook1 = app.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, // Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); EXCELAPP.Workbook workbook1 = app.Workbooks.Open(filename, 0, false, 5, "", "", false, EXCELAPP.XlPlatform.xlWindows, "", true, false, 0, true, false, false); //Get all the sheets in the workbook var allWorkSheets = workbook1.Worksheets; try { //Get the allready exists sheet EXCELAPP.Worksheet worksheet = (EXCELAPP.Worksheet)allWorkSheets.get_Item("Sheet1"); //EXCELAPP.Worksheet worksheet = (EXCELAPP.Worksheet)workbook1.ActiveSheet; int totalRows = worksheet.UsedRange.Rows.Count; EXCELAPP.Range oRng = worksheet.UsedRange.get_Range("D1", "D" + totalRows); //Insert the new column, specifying the direction you want to shift existing columns oRng.EntireColumn.Insert(EXCELAPP.XlInsertShiftDirection.xlShiftToRight, Type.Missing); //select the I1 range again. //oRng = worksheet.UsedRange.get_Range("D1", "D" + totalRows); //Set the column header text worksheet.Cells[1, 4] = "新的标题"; //EXCELAPP.Range xlsColumns = // (EXCELAPP.Range)worksheet.Columns[4, Type.Missing]; //xlsColumns.Insert(EXCELAPP.XlInsertShiftDirection.xlShiftToRight, EXCELAPP.XlInsertFormatOrigin.xlFormatFromRightOrBelow); int nColumns = worksheet.UsedRange.Columns.Count; int nRows = worksheet.UsedRange.Rows.Count; for (int i = 2; i < nRows; i++) { var temCellValue = worksheet.UsedRange.Cells[i, 10] as EXCELAPP.Range; if (temCellValue != null && !string.IsNullOrEmpty(temCellValue.Text)) { DateTime startDate; if (DateTime.TryParseExact(temCellValue.Text, "dd/MM/yyyy", null, <span style="white-space:pre"></span>System.Globalization.DateTimeStyles.None, out startDate)) { <pre name="code" class="csharp"><span style="white-space:pre"></span>worksheet.Cells[i, 4] = "新的值";} else { worksheet.Cells[i, 4] = "日期转换错误."; } } } workbook1.Save(); } catch (Exception ex) { //todo log... } finally { if (workbook1 != null) { try { workbook1.Close(false); } catch { } System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbook1); workbook1 = null; } app.Quit(); GC.WaitForPendingFinalizers(); GC.Collect(); } #endregion
0 0
- Interop 修改已存在的EXCEL 新增一列
- PHPExcel 修改已存在Excel
- 打开已存在的Excel
- python写入已存在的excel数据
- 修改已存在文件
- 在oracle中,如何给已存在的表添加一列?
- 判断是否存在某列,并新增一列
- Datatable新增一列 以及 列“XXXX”已属于此 DataTable。
- 用Excel表格检查ArcGIS属性表中一列属性是否存在相同的值
- 数据导入到已存在的excel表格中
- java 向已存在的excel中追加数据
- java 向已存在的excel中追加数据 .
- PostgreSQL新增一列的pl/sql脚本
- oracle中修改已存在数据的列
- oracle中修改已存在数据的列
- 修改已存在用户的所属组(usermod用法)
- Microsoft.Office.Interop.Excel, Version=12.0.0.0版本高于引用的程序集(已解决)
- Mysql 在 mybatis 环境下 批量新增 并 将已存在的数据更新
- 中文乱码之最不易发觉的中文乱码!
- Jprofiler监控JVM性能
- [官翻] 隐藏状态栏
- MySQL优化之四--Mysql基准测试工具(mysqlslap、sysbench)
- [Ubuntu--Install]Ubuntu下安装Sublime Text编辑器
- Interop 修改已存在的EXCEL 新增一列
- 从Message生产到消费的过程理解Handler消息机制
- .mat文件与.txt文件相互转换
- redis使用心得
- binlog实现并发复制过程slave线程数
- mysql复制
- CodeForces 621B Wet Shark and Bishops
- react native 布局篇
- Exception in thread "main" java.lang.UnsupportedClassVersionError: scala/tools/n sc/MainGenericRunne