当前位置: 首页>>代码示例>>C#>>正文


C# Application.Calculate方法代码示例

本文整理汇总了C#中Microsoft.Office.Interop.Excel.Application.Calculate方法的典型用法代码示例。如果您正苦于以下问题:C# Application.Calculate方法的具体用法?C# Application.Calculate怎么用?C# Application.Calculate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Office.Interop.Excel.Application的用法示例。


在下文中一共展示了Application.Calculate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Calc

        public static string Calc(string str)
        {
            string value = "";

            Excel.Application xlApp = null;
            Excel.Workbook wb = null;
            Excel.Worksheet ws = null;

            try
            {
                xlApp = new Excel.Application();
                xlApp.UseSystemSeparators = true;

                /*
                    string culture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();//"en-GB";
                    CultureInfo ci = new CultureInfo(culture);
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("nb-NO");

                    xlApp.UseSystemSeparators = false;
                    xlApp.DecimalSeparator = ci.NumberFormat.NumberDecimalSeparator;
                    xlApp.ThousandsSeparator = ci.NumberFormat.NumberGroupSeparator;

                    System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator = ";";
                */

                xlApp.Visible = false;
                xlApp.DisplayAlerts = false;
                wb = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                ws = (Excel.Worksheet)wb.Worksheets[1];

                //ws.get_Range("A1", "A1").Value2 = str;
                ws.get_Range("A1", "A1").FormulaLocal = str; // enter formula using the local culture

                xlApp.Calculate();
                value = ws.get_Range("A1", "A1").Value2.ToString();

                int eCode = 0;
                if (int.TryParse(value, out eCode))
                {
                    if (eCode == -2146826281) value = "#DIV/0!";
                    else if (eCode == -2146826246) value = "#N/A";
                    else if (eCode == -2146826259) value = "#NAME?";
                    else if (eCode == -2146826288) value = "#NULL!";
                    else if (eCode == -2146826252) value = "#NUM!";
                    else if (eCode == -2146826265) value = "#REF!";
                    else if (eCode == -2146826273) value = "#VALUE!";
                }

                wb.Close(false);
                xlApp.Quit();
            }

            catch (Exception ex)
            {
                value = "Error: " + ex.Message;
            }

            finally
            {

                try
                {
                    wb.Close(false);
                    xlApp.Quit();
                    releaseObject(ws);
                    releaseObject(wb);
                    releaseObject(xlApp);
                }
                catch
                {
                    releaseObject(ws);
                    releaseObject(wb);
                    releaseObject(xlApp);
                }

            }

            return value;
        }
开发者ID:martsve,项目名称:excel,代码行数:79,代码来源:main.cs

示例2: btnExportExel_Click

        private void btnExportExel_Click(object sender, EventArgs e)
        {
            if (XtraMessageBox.Show("Bán có chắc muốn in hóa đơn?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                if ((!string.IsNullOrEmpty(txtForm.Text)) || (!string.IsNullOrEmpty(txtTo.Text)) ||
                (String.CompareOrdinal(txtForm.Text, txtTo.Text)) > 0)
                {
                    XtraMessageBox.Show("Nhập sai", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    //var  app = (Microsoft.Office.Interop.Excel.Application) Marshal.GetActiveObject("Excel.Application");
                    var app = new Microsoft.Office.Interop.Excel.Application {Visible = false};

                    string path = Path.Combine(Environment.CurrentDirectory, @"Yokowo.xls");
                    object missingValue = System.Reflection.Missing.Value;

                    Workbook book = app.Workbooks.Open(path, missingValue,
                                                        false,
                                                        missingValue,
                                                        missingValue,
                                                        missingValue,
                                                        true,
                                                        missingValue,
                                                        missingValue,
                                                        true,
                                                        missingValue,
                                                        missingValue,
                                                        missingValue);



                    var sheet = (Worksheet)book.Worksheets[5];
                    
                    // day
                    Range date = sheet.Range["E8", "E8"];
                    date.Value2 = DateTime.Now.Date.Day;

                    // month
                    Range month = sheet.Range["G8", "G8"];
                    month.Value2 = DateTime.Now.Date.Month;

                    // day
                    Range year = sheet.Range["H8", "H8"];
                    year.Value2 = DateTime.Now.Date.Year;

                    Range cusName = sheet.Range["F12", "F12"];
                    cusName.Value2 = txtCusName.Text;


                    // cust name
                    Range custName = sheet.Range["D13", "D13"];
                    custName.Value2 = txtCustName.Text;

                    // address
                    Range address = sheet.Range["D14", "D14"];
                    address.Value2 = txtAddress.Text;

                    // tax code
                    Range taxCode = sheet.Range["D15", "D15"];
                    taxCode.Value2 = txtTaxCode.Text;

                    // Del Term
                    Range delTerm = sheet.Range["E16", "E16"];
                    delTerm.Value2 = txtDelTerm.Text;

                    // Del Place
                    Range delPlace = sheet.Range["D17", "D17"];
                    delPlace.Value2 = txtDelPlace.Text;

                    // name buyer
                    Range buyerName = sheet.Range["B18", "B18"];
                    buyerName.Value2 = txtBuyerName.Text;

                    // Tel
                    Range tel = sheet.Range["F18", "F18"];
                    tel.Value2 = txtTel.Text;

                    // fax
                    Range fax = sheet.Range["H18", "H18"];
                    fax.Value2 = txtFax.Text;

                    // pay type
                    Range payType = sheet.Range["G49", "G49"];
                    payType.Value2 = txtPayType.Text;

                    // pay term
                    Range payTerm = sheet.Range["G51", "G51"];
                    payTerm.Value2 = txtPayTerm.Text;

                    // currency
                    Range currency = sheet.Range["I52", "I52"];
                    currency.Value2 = _customer.CURRENCY;


                    //book.RefreshAll();
                    app.Calculate();
                    book.Save();
                    book.Close(true, null, null);
                    app.Quit();
//.........这里部分代码省略.........
开发者ID:cuongpv88,项目名称:work,代码行数:101,代码来源:FormInvoice.cs

示例3: Main


//.........这里部分代码省略.........
                        cellB[0] = r.Rows.Count;
                    }

                    // select and paste values
                    try
                    {
                        startCell = (Excel.Range)oSheet.Cells[cellA[0], cellA[1]];
                        endCell = (Excel.Range)oSheet.Cells[cellB[0], cellB[1]];
                        oSheet.get_Range(startCell, endCell).Value = null;

                        endCell = (Excel.Range)oSheet.Cells[cellA[0] + N - 1, cellA[1] + C - 1];

                        if (asText) oSheet.get_Range(startCell, endCell).Value2 = sCells;
                        else oSheet.get_Range(startCell, endCell).Value2 = cells;
                    }
                    catch (Exception e)
                    {
                        string inputCell = GetExcelColumnName(cellA[1]) + cellA[0] + ":" + GetExcelColumnName(cellB[1]) + cellB[0];
                        throw new System.Exception("Unable to select input cells:\n\n         " + inputCell + "\n\n" + errLine + "\n\n" + e.ToString());
                    }
                }

                // run macro
                for (int i = 0; i < macro.Count; i++)
                {
                    try
                    {
                        oXL.Run(macro[i]);
                    }
                    catch (Exception e) { throw new System.Exception("Unable to run macro: " + macro[i] + "\n\n" + errLine + "\n\n" + e.ToString()); }
                }

                // force workbook refresh
                oXL.Calculate();

                // go to result sheet
                if (sheet.Length > 0)
                {
                    try
                    {
                        oSheet = (Excel._Worksheet)oWB.Sheets[sheet];
                    }
                    catch (Exception e) { throw new System.Exception("Unable to select output sheet:" + sheet + "\n\n" + errLine + "\n\n" + e.ToString()); }
                }

                // save charts

                foreach (Excel.Worksheet cSheet in oWB.Worksheets)
                {
                    // loop trough all charts

                    Excel.ChartObjects xlCharts = (Excel.ChartObjects)cSheet.ChartObjects(Type.Missing);
                    for (int i = 1; i <= xlCharts.Count; i++)
                    {

                        Excel.ChartObject oChart = (Excel.ChartObject)xlCharts.Item(i);
                        Excel.Chart chart = oChart.Chart;

                        string chartName = "";
                        if (charts.Contains(cSheet.Name + "." + oChart.Name)) chartName = cSheet.Name + "." + oChart.Name;
                        else if (charts.Contains(oChart.Name)) chartName = oChart.Name;

                        // if chart is specified for output, save it
                        if (chartName.Length > 0)
                        {
                            int id = charts.FindIndex(s => s == chartName);
开发者ID:martsve,项目名称:excel,代码行数:67,代码来源:main.cs


注:本文中的Microsoft.Office.Interop.Excel.Application.Calculate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。