using
Microsoft.Office.Interop.Excel;
using
Excel=Microsoft.Office.Interop.Excel;
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture =
new
System.Globalization.CultureInfo(
"en-US"
);
Excel.Application xlApp =
new
Excel.Application();
excelApp.DisplayAlerts =
false
;
excelApp.Visible =
false
;
excelApp.ScreenUpdating =
false
;
Excel.Workbook xlsWorkBook = excelApp.Workbooks.Open(path, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
Excel.Worksheet xlsWorkSheet = (Worksheet)xlsWorkBook.Worksheets[
"sales plan"
];
***获取最后一行、一列的两种方法***
int
rowsCount = xlsWorkSheet.UsedRange.Rows.Count;
int
colsCount = xlsWorkSheet.UsedRange.Columns.Count;
int
rowsCount = xlsWorkSheet.get_Range(
"A65536"
,
"A65536"
).get_End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row;
int
colsCount = xlsWorkSheet.get_Range(
"ZZ1"
,
"ZZ1"
).get_End(Microsoft.Office.Interop.Excel.XlDirection.xlToLeft).Column;
***将Excel数据存入二维数组***
Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Cells[1, 1];
Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Cells[rowsCount, colsCount];
Range rng = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.get_Range(c1, c2);
object
[,] exceldata = (
object
[,])rng.get_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault);
Excel.Range xlsColumns = (Excel.Range)xlsWorkSheet.Columns[1, System.Type.Missing];
xlsColumns.Insert(XlInsertShiftDirection.xlShiftToRight, Type.Missing);
Excel.Range rng;
rng = worksheet.get_Range(
"A:A"
,
"A:A"
);
rng.Insert(Excel.XlDirection.xlToRight, Excel.XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
Excel.Range rng = (Microsoft.Office.Interop.Excel.Range)xlsWorkSheet.Columns[12, Type.Missing];
rng.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
Range deleteRng = (Range)xlsWorkSheetSapExcel.Rows[2, System.Type.Missing];
deleteRng.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 11]).Select();
((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 11]).EntireColumn.Delete(0);
((Excel.Range)xlsSheetShareMajor_Meisai.Cells[1, 3]).EntireColumn.Delete (0);
xlsWorkSheet.get_Range(
"A1"
,
"A1"
).Interior.ColorIndex = 3;
Microsoft.Office.Interop.Excel.Range range1 = xlsWorkSheetAdd.get_Range(
"J1"
,
"J65535"
);
range1.NumberFormat =
"@"
;
range1 = xlsWorkSheetAdd.get_Range(
"L1"
,
"L65535"
);
range1.NumberFormat =
"0.00"
;
Excel.Range rng = xlsSheetShareMajor_Meisai.Columns[
"I"
, System.Type.Missing]
as
Excel.Range;
rng.NumberFormatLocal =
@"yyyy/m/d"
;
Range Drng = xlsWorkSheetTemplate.get_Range(
"D1"
,
"D65535"
);
Drng.Replace(
" "
,
""
, XlLookAt.xlPart, XlSearchOrder.xlByColumns, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
Drng.TextToColumns(Drng, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierSingleQuote, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
Range TextToColumnRng = xlsWorkSheet.get_Range(
"E1"
,
"E65535"
);
xlsWorkSheet.get_Range(
"E1"
,
"E65535"
).TextToColumns(TextToColumnRng, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierSingleQuote, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
rng = xlMergeFileWorkSheet.get_Range(
"D2"
,
"D"
+ rowcount);
rng.Formula =
@"=IF(RC[-3]=""H"",""Survivor"",""Donor"")"
;
copyRng = xlsSheetTemplateMajor_Meisai.get_Range(
"N3"
,
"N"
+ lastRowTemplate);
copyRng.Formula =
"=VLOOKUP(RC[-12],AR残!C[-13]:C[-11],2,0)"
;
string
f = Convert.ToString(xlsSheetShareMajor_Meisai.get_Range(xlsSheetShareMajor_Meisai.Cells[2, 1], xlsSheetShareMajor_Meisai.Cells[2,1]).Value2);
D1_rng = D1_TemSheet.Cells.get_Range(D1_TemSheet.Cells[1, 1], D1_TemSheet.Cells[1, 50]);
rng.AutoFilter(5,
"S"
, Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing,
true
);
rng.AutoFilter(6,
"H"
, Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlOr,
"F"
,
true
);
D1_rng.AutoFilter(D1_Column + 2,
"#N/A"
, Microsoft.Office.Interop.Excel.XlAutoFilterOperator.xlFilterValues,
"#N/A"
,
false
);
D2_rng.Copy(Type.Missing);
D2_TemSheet.Cells.get_Range(D2_TemSheet.Cells[2, (D2_Column + 1)], D2_TemSheet.Cells[2, (D2_Column + 1)]).PasteSpecial(Excel.XlPasteType.xlPasteValues, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone,
false
,
false
);
rng = mySheet.get_Range(
"A1"
,
"IV10"
).Find(arrLabel[j], Type.Missing,
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext,
false
, Type.Missing, Type.Missing);
range.EntireColumn.AutoFit();
xlsWorkBook.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
***关闭对象***
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsWorkBook);
excelApp.Quit();
Kill(excelApp);
GC.Collect();
/// <summary>
/// 关闭Excel进程
/// </summary>
public
class
KeyMyExcelProcess
{
[DllImport(
"User32.dll"
, CharSet = CharSet.Auto)]
public
static
extern
int
GetWindowThreadProcessId(IntPtr hwnd,
out
int
ID);
public
static
void
Kill(Microsoft.Office.Interop.Excel.Application excel)
{
try
{
IntPtr t =
new
IntPtr(excel.Hwnd);
int
k = 0;
GetWindowThreadProcessId(t,
out
k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
catch
(System.Exception ex)
{
throw
ex;
}
}
}
public
void
CloseExcel(Microsoft.Office.Interop.Excel.Application ExcelApplication, Microsoft.Office.Interop.Excel.Workbook ExcelWorkbook)
{
ExcelWorkbook.Close(
false
, Type.Missing, Type.Missing);
ExcelWorkbook =
null
;
ExcelApplication.Quit();
GC.Collect();
KeyMyExcelProcess.Kill(ExcelApplication);
}