标签: excel-interop

Excel Interop 只读取筛选的行

原始未过滤表

原始未过滤表

过滤表

在此输入图像描述

我正在尝试使用 Interop.Excel 读取 .xlsx 文件。当我将xlRange变量设置为仅显示过滤后单元格(可见)时,它似乎有一个奇怪的行为:

Excel.Range xlRange = xlWorksheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);

调试:

当表没有过滤时:

xlRange.Count: 15 //表中元素总数

rowCount: 5 //这包括标题

当表被过滤时:

xlRange.Count: 9 //这是正确的

rowCount: 1 //这应该是3(包括标题)

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(Directory.GetCurrentDirectory() + "\\Example.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Type.Missing);

int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;

//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for …
Run Code Online (Sandbox Code Playgroud)

c# excel office-interop excel-interop

3
推荐指数
1
解决办法
908
查看次数

Windows Server 2008上是否支持Office 2003互操作..?

尝试在Windows Server 2008上运行互操作依赖程序时,我遇到错误,它在Win Server 2003和XP上正常运行:

System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:Microsoft.Office.Interop.Excel.WorkbookClass.SaveAs(.......)中的0x800A03EC

根据微软的说法,Windows Server 2008不在支持的操作系统列表中.

http://www.microsoft.com/downloads/en/details.aspx?familyid=3c9a983a-ac14-4125-8ba0-d36d67e0f4ad&displaylang=en

系统要求

支持的操作系统:Windows 2000 Service Pack 3; Windows Server 2003; Windows XP

任何人都可以证实这一点..有没有人试过..?

.net excel-interop

2
推荐指数
1
解决办法
5237
查看次数

如何在Excel Interop C#中读取DateTime

可能重复:
从Excel工作表读取日期时间值

我正在尝试使用C#中的Interop从Excel电子表格中读取DateTime值.我一直都在表单的"C"栏中.我的代码如下:

    public void addTime(Microsoft.Office.Interop.Excel.Workbook workbook)
    {

        Excel.Worksheet ws = (Excel.Worksheet)workbook.Worksheets.get_Item("Time Series");
        Excel.Range range = ws.UsedRange;
        int num = 0;
        for (int row = 1; row <= range.Rows.Count; row++ )
        {
            String dtString = ((Excel.Range)ws.Cells[row, "C"]).Value2.ToString();
            DateTime dt = Convert.ToDateTime(dtString);

            this.addEdgeInstance(dt);
        }
    }
Run Code Online (Sandbox Code Playgroud)

然而,这在当时没有读到.读取字符串有效,但Convert函数不起作用.我是否以不同的方式阅读了价值观?值可以变化吗?(IE:我可以输入'11/11'或'11/11/2011',因为Excel在Excel中工作时将这两个条目识别为valie DateTime值吗?)

c# excel excel-interop

2
推荐指数
1
解决办法
1万
查看次数

从Excel SpreadSheet读取数据 - 无法对空引用执行运行时绑定

我有以下代码从excel文件中读取数据,我遇到的问题是,当我尝试读取范围数据时,我得到一个异常"无法在此行上对空引用执行运行时绑定"

if (Int32.TryParse(xlRange.Cells[1, 4].Value2.ToString(), out value)) 
Run Code Online (Sandbox Code Playgroud)

我想知道的是我如何正确访问该范围内的信息.提前致谢

void ReadFromExcelToGrid2(String fileNameString)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileNameString);
                //get list of worksheets associated with the current workbook
                Microsoft.Office.Interop.Excel.Sheets worksheets = xlWorkbook.Worksheets;
                //list the work books in a dropdownlist 
                IDictionary<int, String> sheets = new Dictionary<int, String>();
                foreach (Microsoft.Office.Interop.Excel.Worksheet displayWorksheet in xlWorkbook.Worksheets)
                {
                    sheets.Add(displayWorksheet.Index, displayWorksheet.Name);
                }
                cmbWorksheets.DataSource = new BindingSource(sheets, null);;
                cmbWorksheets.DisplayMember = "Value";
                cmbWorksheets.ValueMember = "Key";

                Microsoft.Office.Interop.Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[4]; // assume it is the first sheet …
Run Code Online (Sandbox Code Playgroud)

c# excel ms-office excel-interop

2
推荐指数
1
解决办法
7796
查看次数

如何以编程方式检索Excel工作表中的页数?

我不是在寻找工作表的数量.我正在寻找每个工作表中的总页数.

.net c# excel worksheet excel-interop

2
推荐指数
1
解决办法
411
查看次数

使用Excel Interop w/C#中心Excel单元格内容

我试图将将用作标题的单元格的值居中.这是我目前用于格式化这些单元格的代码.我正在使用Excel Interop和C#.如何将此范围的单元格中的值居中.

public void createHeaders(int row, int col, string htext, string cell1,
    string cell2, int mergeColumns, string b, bool font, int size, string
    fcolor)
    {
        worksheet.Cells[row, col] = htext;
        workSheet_range = worksheet.get_Range(cell1, cell2);
        workSheet_range.Merge(mergeColumns);
        switch (b)
        {
            case "BLUE":
                workSheet_range.Interior.Color = System.Drawing.Color.Red.ToArgb();
                break;
            case "GAINSBORO":
                workSheet_range.Interior.Color =
        System.Drawing.Color.Gainsboro.ToArgb();
                break;
            //case "Turquoise":
               // workSheet_range.Interior.Color =
        //System.Drawing.Color.Turquoise.ToArgb();
                //break;
            case "PeachPuff":
                workSheet_range.Interior.Color =
        System.Drawing.Color.PeachPuff.ToArgb();
                break;
            default:
                //  workSheet_range.Interior.Color = System.Drawing.Color..ToArgb();
                break;
        }

        //workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
        //workSheet_range.Borders = null;
        workSheet_range.Font.Bold = font;
        workSheet_range.ColumnWidth = …
Run Code Online (Sandbox Code Playgroud)

c# excel excel-interop

2
推荐指数
1
解决办法
1万
查看次数

如何设置Excel窗口的大小?

我有一个wpf窗口,应该在Excel窗口旁边打开。Excel是使用Interop.Excel打开并处理的。在我的窗口中,我有一个方法,应该设置excel窗口的大小。

private void SetLayout()
        {
            Top = 0;
            Left = 0;
            Height = SystemParameters.WorkArea.Height;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Height =
                SystemParameters.WorkArea.Height;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Left = Width;
            ((ConfiguratorWindowViewModel) DataContext).Manager.App.Width = SystemParameters.WorkArea.Width - Width;
        }
Run Code Online (Sandbox Code Playgroud)

在我的Viewmodel中,我有一个Manager,该Manager会返回打开的Excel App(Office.Interop.Excel.Application)。我可以在wpf窗口中设置Height,Top,..,它可以正常工作,但不能在Excel窗口中。我如何在wpf窗口旁边设置Excel窗口的大小,以便两者一起填满整个屏幕?

编辑:

我试过了,但是也没有用:

((ConfiguratorWindowViewModel) DataContext).Manager.App.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlNormal;
((ConfiguratorWindowViewModel) DataContext).Manager.App.ActiveWindow.Height = Screen.PrimaryScreen.Bounds.Height;
((ConfiguratorWindowViewModel) DataContext).Manager.App.ActiveWindow.Width = Screen.PrimaryScreen.Bounds.Width - Width;
Run Code Online (Sandbox Code Playgroud)

当我对其进行硬编码时,两者都将Height设置为不同的值,但没有设置为正确的值,因为.Height=800;它也无法正常工作。

不幸的是,我无法发布图像来显示外观和外观。

编辑:

问题与这个问题不同(如何更改另一个程序的窗口大小?)。我不想更改“外国”应用程序的大小。

c# wpf excel office-interop excel-interop

2
推荐指数
1
解决办法
3895
查看次数

为什么最后一行永远不会被读取?

您好我有一个问题为什么最后一行永远不会被读取?如果excel文件中只有一行或100行,则无关紧要.最后一行永远不会出现在列表中.我不知道为什么......

这是我的Excel文件: Excel文件

这是我的方法:

public List<string> getListData(bool skipFirstRow, int numberOfColumns, string filepath)
{
    int startpoint = 1;
    int cell = 1;
    int row = 1;

    List<string> stringList = new List<string>();

    //Open Excel (Application)
    var excelApplication = openExcelApplication();
    //Open Excel File
    Excel.Workbook excelWorkbook = excelApplication.Workbooks.Open(filepath);
    //Get the Worksheets from the file
    Excel.Sheets excelSheets = excelWorkbook.Worksheets;
    //Select the first Worksheet
    Excel.Worksheet worksheet = (Excel.Worksheet)excelSheets.get_Item(1);

    if (skipFirstRow == true)
    {
        startpoint = 2;
    }

    Excel.Range range = worksheet.get_Range("A" + Convert.ToString(startpoint), Missing.Value);

    while ((range.Cells[startpoint, cell] …
Run Code Online (Sandbox Code Playgroud)

c# excel excel-interop

2
推荐指数
1
解决办法
267
查看次数

使用C#从Excel读取单元格的方法是什么?

如果我们想要读取或写入excel中的单元格,我发现了人们使用的两种方式.

宣言:

Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook srcWorkBook = ExcelApp.Workbooks.Open(@"C:\test.xls");
Excel.Worksheet srcWorkSheet = srcWorkBook.Worksheets[1];
Excel.Range srcRange = srcWorkSheet.UsedRange;
Run Code Online (Sandbox Code Playgroud)

用法1:

srcWorkSheet.Cells[1, 1].value2 = "foo bar";
Run Code Online (Sandbox Code Playgroud)

用法2:

srcRange.Cells[2, 2].Value2 = "foo bar";
Run Code Online (Sandbox Code Playgroud)

哪一个是最好的使用方式?或者它在.NET中都很好?

c# excel excel-interop

2
推荐指数
1
解决办法
78
查看次数

为什么并行化代码不会写入Excel电子表格?

Excel电子表格中编写许多工作表可能需要一段时间.并行化它会很有帮助.

此代码工作得很好,它使一个Excel电子表格的流行有四个工作表在屏幕上名为Sheet1,1,2,和3.

open Microsoft.Office.Interop.Excel
open FSharp.Collections.ParallelSeq

let backtestWorksheets = [1..3]

let app = new ApplicationClass(Visible = true) 

let workbook = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)

let writeInfoSheet (worksheet: Worksheet) : unit =

    let foo i =
        let si = string i
        worksheet.Range("A" + si, "A" + si).Value2 <- "Hello " + si
    List.iter foo [1..10]

let wfm = [1, writeInfoSheet; 2, writeInfoSheet; 3, writeInfoSheet]
          |> Map.ofList

let adder (workbook : Workbook)
          (i …
Run Code Online (Sandbox Code Playgroud)

parallel-processing excel f# excel-interop

2
推荐指数
1
解决办法
117
查看次数