| |

VerySource

 Forgot password?
 Register
Search
View: 863|Reply: 6

How to call Excel function in C #?

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-2-7 11:30:01
| Show all posts |Read mode
Just add a reference in VB
Then write
Excel.Application.WorksheetFunction.GAMMALN (10) is enough, but how to implement it in C #?
Reply

Use magic Report

0

Threads

31

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-3-28 02:15:02
| Show all posts
Add COM reference
Then instantiate it like a class in C #, use
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-3-31 15:00:01
| Show all posts
Can you be specific?
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-4-4 23:45:01
| Show all posts
The operation of EXCEL in C # is a bit different from that of VB.NET. Pay special attention to that C # must fill in each parameter of the calling method, which cannot be omitted, but VB.NET can.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-4-5 14:45:01
| Show all posts
using System;
using System.Data;
using Excel;

namespace LogicLayer
{
///
/// Summary description of OutputExcel
///
public class OutputExcel
{
public OutputExcel (DataView dv, string str)
{
//
// TODO: Add constructor logic here
//
Excel.Application excel;
int rowIndex = 4;
int colIndex = 1;

Excel._Workbook xBk;
Excel._Worksheet xSt;

excel = new Excel.ApplicationClass () ;;
xBk = excel.Workbooks.Add (true);
xSt = (Excel._Worksheet) xBk.ActiveSheet;

//
// Get the title
//
foreach (DataColumn col in dv.Table.Columns)
{
colIndex ++;
excel.Cells [4, colIndex] = col.ColumnName;
xSt.get_Range (excel.Cells [4, colIndex], excel.Cells [4, colIndex]). HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; // Set the title format to center alignment
}

//
// Get the data in the table
//
foreach (DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach (DataColumn col in dv.Table.Columns)
{
colIndex ++;
if (col.DataType == System.Type.GetType ("System.DateTime"))
{
excel.Cells [rowIndex, colIndex] = (Convert.ToDateTime (row [col.ColumnName] .ToString ())). ToString ("yyyy-MM-dd");
xSt.get_Range (excel.Cells [rowIndex, colIndex], excel.Cells [rowIndex, colIndex]). HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; // Set the date field format to center alignment
}
else
if (col.DataType == System.Type.GetType ("System.String"))
{
excel.Cells [rowIndex, colIndex] = "'" + row [col.ColumnName] .ToString ();
xSt.get_Range (excel.Cells [rowIndex, colIndex], excel.Cells [rowIndex, colIndex]). HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; // Set the character field format to center alignment
}
else
{
excel.Cells [rowIndex, colIndex] = row [col.ColumnName] .ToString ();
}
}
}
//
// Load a total line
//
int rowSum = rowIndex + 1;
int colSum = 2;
excel.Cells [rowSum, 2] = "Total";
xSt.get_Range (excel.Cells [rowSum, 2], excel.Cells [rowSum, 2]). HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
//
// Set the color of the selected part
//
xSt.get_Range (excel.Cells [rowSum, colSum], excel.Cells [rowSum, colIndex]). Select ();
xSt.get_Range (excel.Cells [rowSum, colSum], excel.Cells [rowSum, colIndex]). Interior.ColorIndex = 19; // Set to light yellow, there are 56 kinds in total
//
// Get the title of the entire report
//
excel.Cells [2,2] = str;
//
// Set the title format of the entire report
//
xSt.get_Range (excel.Cells [2,2], excel.Cells [2,2]). Font.Bold = true;
xSt.get_Range (excel.Cells [2,2], excel.Cells [2,2]). Font.Size = 22;
//
// Set the report table to the most suitable width
//
xSt.get_Range (excel.Cells [4,2], excel.Cells [rowSum, colIndex]). Select ();
xSt.get_Range (excel.Cells [4,2], excel.Cells [rowSum, colIndex]). Columns.AutoFit ();
//
// Set the title of the entire report to be centered across columns
//
xSt.get_Range (excel.Cells [2,2], excel.Cells [2, colIndex]). Select ();
xSt.get_Range (excel.Cells [2,2], excel.Cells [2, colIndex]). HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
//
// Draw border
//
xSt.get_Range (excel.Cells [4,2], excel.Cells [rowSum, colIndex]). Borders.LineStyle = 1;
xSt.get_Range (excel.Cells [4,2], excel.Cells [rowSum, 2]). Borders [Excel.XlBordersIndex.xlEdgeLeft] .Weight = Excel.XlBorderWeight.xlThick; // Set the left line bold
xSt.get_Range (excel.Cells [4,2], excel.Cells [4, colIndex]). Borders [Excel.XlBordersIndex.xlEdgeTop] .Weight = Excel.XlBorderWeight.xlThick; // Set the upper line bold
xSt.get_Range (excel.Cells [4, colIndex], excel.Cells [rowSum, colIndex]). Borders [Excel.XlBordersIndex.xlEdgeRight] .Weight = Excel.XlBorderWeight.xlThick; // Set the right line bold
xSt.get_Range (excel.Cells [rowSum, 2], excel.Cells [rowSum, colIndex]). Borders [Excel.XlBordersIndex.xlEdgeBottom] .Weight = Excel.XlBorderWeight.xlThick; // Set the bottom line bold
//
//display effect
//
excel.Visible = true;
}
}
}
Also: don't forget the KILL EXCEL process
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-4-5 15:45:01
| Show all posts
You need to declare an excel instance first
such as:
Microsoft.Office.Interop.Excel.ApplicationClass excel =
new Microsoft.Office.Interop.Excel.ApplicationClass ();
-Then you can call the function
bool b = excel.WorksheetFunction.IsNumber ("ABC");
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-9 11:00:01
| Show all posts
There are many ways to use late binding or to reference COM.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list