- using System;
- using System.Collections;
- using Excel = Microsoft.Office.Interop.Excel;
- namespace ExcelEdit
- {
- class ExcelEdit2
- {
-
-
-
- public class JointExcel
- {
- #region 私有成员
- private Excel.ApplicationClass m_objExcel;
- private Excel.Workbooks m_objBooks;
- private Excel.Workbook m_objBook;
- private Excel.Worksheet m_objSheet;
- private Excel.Range m_Range;
- private System.Reflection.Missing miss =
- System.Reflection.Missing.Value;
- private Excel.Font m_Font;
- private Excel.Borders m_Borders;
-
- private Excel.Border m_BorderTop;
- private Excel.Border m_BorderBottom;
- private Excel.Border m_BorderLeft;
- private Excel.Border m_BorderRight;
- private Excel.Range m_cellRange;
-
- private string[] m_colString = new string[26] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
- #endregion
-
-
-
-
- public JointExcel()
- {
- m_objExcel = new Excel.ApplicationClass();
- m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
- m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
- m_objSheet = (Excel.Worksheet)m_objBook.ActiveSheet;
- }
- ~JointExcel()
- {
-
- if (m_cellRange != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_cellRange);
- if (m_BorderTop != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_BorderTop);
- if (m_BorderBottom != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_BorderBottom);
- if (m_BorderLeft != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_BorderLeft);
- if (m_BorderRight != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_BorderRight);
- if (m_Borders != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Borders);
- if (m_Font != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Font);
- if (m_Range != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Range);
- if (m_objSheet != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
- if (m_objBook != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
- if (m_objBooks != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
- if (m_objExcel != null)
- System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
- GC.Collect();
- }
- #region 选定单元格
- private string GetCell(int ColNum, int RowNum)
- {
- string temp = "A";
- int row = RowNum + 1;
- if (ColNum < 0 || ColNum > 255)
- {
- throw new Exception("行号错误");
- }
- int i0, i1 = 0;
- i0 = Math.DivRem(ColNum, 25, out i1);
- if (i0 == 0 && i1 == 0)
- {
- return "A" + row.ToString();
- }
- if (i0 == 0 && i1 > 0)
- {
- return m_colString[i1] + row.ToString();
- }
- else
- {
-
- return m_colString[i0] + m_colString[i1] + row.ToString();
- }
- }
-
-
-
-
-
- public void SetRange(int ColNum, int RowNum)
- {
- m_Range = m_objSheet.get_Range((object)GetCell(ColNum, RowNum), miss);
- m_Font = m_Range.Font;
- m_Borders = m_Range.Borders;
- m_BorderTop = m_Borders[Excel.XlBordersIndex.xlEdgeTop];
- m_BorderBottom = m_Borders[Excel.XlBordersIndex.xlEdgeBottom];
- m_BorderLeft = m_Borders[Excel.XlBordersIndex.xlEdgeLeft];
- m_BorderRight = m_Borders[Excel.XlBordersIndex.xlEdgeRight];
- m_cellRange = m_Range;
- }
-
-
-
-
-
-
-
- public void SetRange(int startColNum, int startRowNum, int endColNum, int
- endRowNum)
- {
- m_Range =
- m_objSheet.get_Range((object)GetCell(startColNum, startRowNum), (object)GetCell(endColNum, endRowNum));
- m_Font = m_Range.Font;
- m_Borders = m_Range.Borders;
- m_BorderTop = m_Borders[Excel.XlBordersIndex.xlEdgeTop];
- m_BorderBottom = m_Borders[Excel.XlBordersIndex.xlEdgeBottom];
- m_BorderLeft = m_Borders[Excel.XlBordersIndex.xlEdgeLeft];
- m_BorderRight = m_Borders[Excel.XlBordersIndex.xlEdgeRight];
- m_cellRange = m_Range;
- }
- #endregion
-
- #region 给单元格附值
-
-
-
-
- public void SetCellValue(string value)
- {
- if (m_Range == null) throw new System.Exception("没有设定单元格或者区域");
- m_Range.Value2 = value;
- }
-
-
-
-
-
-
- public void SetCellValue(int row, int col, string value)
- {
- SetRange(col, row);
- m_Range.Value2 = value;
- m_Range.Font.Name = "Arial";
- m_Range.Font.Size = 9;
- }
-
-
-
-
-
-
-
-
- public void SetCellValue(int startRow, int startCol, int endRow, int
- endCol, string value)
- {
- Merge(startRow, startCol, endRow, endCol);
- m_Range.Value2 = value;
- m_Range.Font.Size = 9;
- m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
- }
- #endregion
- public void SetCellbolk(int row, int col)
- {
- SetRange(col, row);
- m_Range.Font.Bold = true;
- }
- #region 设定单元格对齐方式
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
- #region 设置行高和列宽
-
-
-
-
- public void SetColumnWidth(float columnWidth)
- {
- m_Range.ColumnWidth = columnWidth;
- }
-
-
-
-
-
- public void SetColumnWidth(int columnIndex, float columnWidth)
- {
- SetRange(columnIndex, 0);
- m_Range.ColumnWidth = columnWidth;
- }
-
-
-
-
- public void SetRowHeigh(float rowHeigh)
- {
- m_Range.RowHeight = rowHeigh;
- }
-
-
-
-
-
- public void SetRowHeigh(int rowIndex, float rowHeigh)
- {
- SetRange(0, rowIndex);
- m_Range.RowHeight = rowHeigh;
- }
- #endregion
- #region 合并单元格
-
-
-
- public void Merge()
- {
- m_Range.Merge(null);
- }
-
-
-
-
-
-
-
- public void Merge(int startRowIndex, int startColumnIndex, int endRowIndex,
- int endColumnIndex)
- {
- SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
- m_Range.Merge(null);
- }
- #endregion
- #region 设置字体名称、大小
-
-
-
-
-
-
-
-
- public void SetFont(int startRowIndex, int startColumnIndex, int endRowIndex,
- int endColumnIndex, string fontName)
- {
- SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
- m_Font.Name = fontName;
- }
-
-
-
-
-
-
-
-
- public void SetFont(int startRowIndex, int startColumnIndex, int endRowIndex,
- int endColumnIndex, int fontSize)
- {
- SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
- m_Font.Size = fontSize;
- }
-
-
-
-
-
-
-
-
-
- public void SetFont(int startRowIndex, int startColumnIndex, int endRowIndex,
- int endColumnIndex, string fontName, int fontSize)
- {
- SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
- m_Font.Name = fontName;
- m_Font.Size = fontSize;
- }
-
-
-
-
-
-
-
- public void SetFont(int rowIndex, int columnIndex, string fontName, int fontSize)
- {
- SetRange(columnIndex, rowIndex);
- m_Font.Name = fontName;
- m_Font.Size = fontSize;
- }
-
-
-
-
-
-
- public void SetFont(int rowIndex, int columnIndex, string fontName)
- {
- SetRange(columnIndex, rowIndex);
- m_Font.Name = fontName;
- }
-
-
-
-
- public void SetFont(int rowIndex, int columnIndex, int fontSize)
- {
- SetRange(columnIndex, rowIndex);
- m_Font.Size = fontSize;
- }
-
-
-
-
- public void SetFont(string fontName)
- {
- m_Font.Name = fontName;
- }
- #endregion
- public void setcolor(int rowSum, int colSum, int endrowSum, int endcolIndex, int color)
- {
- m_objSheet.get_Range(m_objExcel.Cells[rowSum, colSum], m_objExcel.Cells[endrowSum, endcolIndex]).Select();
- m_objSheet.get_Range(m_objExcel.Cells[rowSum, colSum], m_objExcel.Cells[endrowSum, endcolIndex]).Interior.ColorIndex = color;
- }
-
- public void setline(int row, int col)
- {
- SetRange(col, row);
- m_Range.Borders.LineStyle = 1;
- }
- public void setline(int srow, int scol, int erow, int ecol, int linetype)
- {
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders.LineStyle = linetype;
- }
- public void setlinebold(int srow, int scol, int erow, int ecol, int linetype)
- {
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders.LineStyle = linetype;
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThick;
- }
- public void setline_left(int srow, int scol, int erow, int ecol, int linetype)
- {
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = linetype;
- }
- public void setline_right(int srow, int scol, int erow, int ecol, int linetype)
- {
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = linetype;
- }
- public void setline_top(int srow, int scol, int erow, int ecol, int linetype)
- {
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = linetype;
- }
- public void setline_btoon(int srow, int scol, int erow, int ecol, int linetype)
- {
- m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = linetype;
- }
-
- public void SetCellValue2(int srow, int scol, int erow, int ecol, string value)
- {
- m_Range = m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]);
- m_Range.Value2 = value;
- m_Range.Font.Name = "Arial";
- m_Range.Font.Size = 9;
- }
- public void SetCellbolk2(int srow, int scol, int erow, int ecol)
- {
- m_Range = m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]);
- m_Range.Font.Bold = true;
- }
- public void save()
- {
- m_objBook.SaveAs(@"E:\Demo.xls", miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
- m_objBook.Close(miss, miss, miss);
- m_objExcel.Quit();
- GC.Collect();
- }
- }
- }
- }
posted @
2011-06-22 18:08
许明吉博客
阅读(
628)
评论()
编辑
收藏
举报