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
原创粉丝点击