1. 创建一个新excel的application:
application exc = new application();
if (exc == null) {
console.writeline("error: excel couldn't be started");
return 0;
}
2. 让这个工程可见:
exc.set_visible(0, true);
3. 获取workbooks集合:
workbooks workbooks = exc.workbooks;
4. 加入新的workbook:
_workbook workbook = workbooks.add(xlwbatemplate.xlwbatworksheet, 0);
5. 获取worksheets集合:
_worksheet worksheet = (_worksheet) sheets.get_item(1);
if (worksheet == null) {
console.writeline ("error in worksheet == null");
}
6. 给单元格设置变量:
range range1 = worksheet.get_range("c1", missing.value);
if (range1 == null) {
console.writeline ("error: range == null");
}
const int ncells = 1;
object[] args1 = new object[1];
args1[0] = ncells;
range1.gettype().invokemember("value", bindingflags.setproperty, null, range1, args1);
例程:
using system;
using system.reflection;
using system.runtime.interopservices;
using excel;
class excel {
public static int main() {
application exc = new application();
if (exc == null) {
console.writeline("error: excel couldn't be started!");
return 0;
}
exc.set_visible(0, true);
workbooks workbooks = exc.workbooks;
_workbook workbook = workbooks.add(xlwbatemplate.xlwbatworksheet, 0);
sheets sheets = workbook.worksheets;
_worksheet worksheet = (_worksheet) sheets.get_item(1);
if (worksheet == null) {
console.writeline ("error: worksheet == null");
}
range range1 = worksheet.get_range("c1", missing.value);
if (range1 == null) {
console.writeline ("error: range == null");
}
const int ncells = 1;
object[] args1 = new object[1];
args1[0] = ncells;
range1.gettype().invokemember("value", bindingflags.setproperty, null,range1, args1);
return 100;
}
}
现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2;
const int ncell = 5;
range range2 = worksheet.get_range("a1", "e1");
int[] array2 = new int [ncell];
for (int i=0; i < array2.getlength(0); i++) {
array2 = i+1;
}
object[] args2 = new object[1];
args2[0] = array2;
range2.gettype().invokemember("value", bindingflags.setproperty, null, range2, args2);
/// <summary>
/// 将指定sheet页的内容完全复制另一sheet页
/// </summary>
/// <param name="beforeSheet">被复制的sheet页 </param>
/// <param name="afterSheet">目标sheet页 </param>
/// <param name="startRow">复制内容在目标sheet页的开始行 </param>
/// <param name="startColumn">复制内容在目标sheet页的开始列 </param>
/// <returns>是否复制成功 </returns>
private bool CopyRange(Excel._Worksheet beforeSheet, Excel._Worksheet afterSheet, int startRow, int startColumn)
{
bool isCopyRange = false;
try
{
Excel.Range beforeRange = beforeSheet.UsedRange;
int rangeRowCount = beforeRange.Rows.Count;
int rangeColumnCount = beforeRange.Columns.Count;
// 空出将写数据集的EXCEL的范围
for (int row = 0; row <= rangeRowCount - 1; row++)
{
Excel.Range rang = afterSheet.get_Range(afterSheet.Cells[startRow, 1],
afterSheet.Cells[startRow, 1]).EntireRow;
//插入一行。
afterSheet.get_Range(afterSheet.Cells[startRow + row, 1],
afterSheet.Cells[startRow + row, 1]).EntireRow.Insert(Excel.XlDirection.xlUp, null);
Excel.Range rangTarget = afterSheet.get_Range(afterSheet.Cells[startRow + row, 1],
afterSheet.Cells[startRow + row, 1]).EntireRow;
rang.Copy(rangTarget);
}
beforeRange.Copy(afterSheet.get_Range(afterSheet.Cells[startRow, startColumn],
afterSheet.Cells[startRow, startColumn + rangeColumnCount]));
isCopyRange = true;
}
catch
{
}
return isCopyRange;
}
#endregion