主函数文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main( )
{
double[] arr=new double [5];
for (int i=0;i<=4;i++){
arr[i]=i;
}
ClassExcel ce = new ClassExcel();
ce.openXlsx("D:\\Sheet1.xlsx");
ce.outputAC(arr, 4, 2, 3);
ce.setBackColor(221, 34, 54);
ce.setWordColor(221, 34, 221);
ce.saveXlsx();
ce.closeXlsx();
//ce.outputNumopen(7);
}
}
}
类文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Drawing;
namespace ConsoleApplication1
{
class ClassExcel
{
public string Fname;
public Microsoft.Office.Interop.Excel.Application xlApp;
public Workbook wb;
public Worksheet ws;
public Range ra;
public ClassExcel()
{
Fname = Environment.CurrentDirectory+"\\"+"workbook1.xlsx";
xlApp = new Microsoft.Office.Interop.Excel.Application();
ra = null;
}
public void setBackColor(int r,int g,int b){
if (ra == null)
{
MessageBox.Show("Range不对", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
ra.Cells.Interior.Color = System.Drawing.Color.FromArgb(r, g, b).ToArgb();
}
public void setWordColor(int r, int g, int b)
{
if (ra == null)
{
MessageBox.Show("Range不对", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
ra.Cells.Font.Color = System.Drawing.Color.FromArgb(r, g, b).ToArgb();
}
public int newXlsx(){
if (xlApp == null)
{
MessageBox.Show("Excel App没有建立", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
ws = (Worksheet)wb.Worksheets[1];
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
return 0;
}
public int openXlsx(string n)
{
if (xlApp == null)
{
MessageBox.Show("Excel App没有建立", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
wb = xlApp.Workbooks.Add(n);
Fname = n;
ws = (Worksheet)wb.Worksheets[1];
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
return 0;
}
public int saveXlsx()
{
if (xlApp == null)
{
MessageBox.Show("Excel App没有建立", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
//设置禁止弹出保存和覆盖的询问提示框
xlApp.DisplayAlerts = false;
xlApp.AlertBeforeOverwriting = false;
//保存工作簿
wb.SaveAs(Fname);
return 0;
}
public int saveAsXlsx(string n)
{
if (xlApp == null)
{
MessageBox.Show("Excel App没有建立", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
//设置禁止弹出保存和覆盖的询问提示框
xlApp.DisplayAlerts = false;
xlApp.AlertBeforeOverwriting = false;
//保存工作簿
wb.SaveAs(n);
return 0;
}
public int outputNum(double dbl,int c,int r)
{
if (xlApp == null)
{
MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
ws.Cells[r,c]=dbl;
// Select the Excel cells, in the range c1 to c7 in the worksheet.
//Range aRange = ws.get_Range("A1", "E1");
//if (aRange == null)
//{
// MessageBox.Show("Range不对", "EXCEL输出",
// MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
// return -1;
//}
//aRange.Font.Size = 15;
//aRange.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb();
return 0;
}
public int outputAC(double[] a,int n,int c,int r)
{
if (xlApp == null)
{
MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
ra = ws.get_Range(ws.Cells[r, c] as Range, ws.Cells[r, c + n]as Range);
for (int i = 0; i <= n; i++)
{
ws.Cells[r, c+i] = a[i];
}
return 0;
}
public int outputAR(double[] a, int n, int c, int r)
{
if (xlApp == null)
{
MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
ra = ws.get_Range(ws.Cells[r, c]as Range, ws.Cells[r+n, c]as Range);
for (int i = 0; i <= n; i++)
{
ws.Cells[r+ i, c ] = a[i];
}
return 0;
}
public int output2DA(double[][] a, int l, int m,int r,int c)
{
if (xlApp == null)
{
MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
ra = ws.get_Range(ws.Cells[r, c]as Range, ws.Cells[r + l, c+m]as Range);
for (int i = 0; i <= l; i++)
{
for (int j = 0; j <= m; j++)
{
ws.Cells[r+i, c+j] = a[i][j];
}
}
return 0;
}
public int output2DAr(double[][] a, int l, int m, int r, int c)
{
if (xlApp == null)
{
MessageBox.Show("EXCEL没打开或创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
if (ws == null)
{
MessageBox.Show("Worksheet创建不成功", "EXCEL输出",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return -1;
}
ra = ws.get_Range(ws.Cells[r, c]as Range, ws.Cells[r + l, c + m]as Range);
for (int i = 0; i <= l; i++)
{
for (int j = m; j <= 0; j--)
{
ws.Cells[r + i, c + j] = a[i][j];
}
}
return 0;
}
public int closeXlsx()
{
wb.Close();
xlApp.Quit();
return 0;
}
}
}