本文介绍了在一张纸中两次显示相同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是在表1中显示数据的代码.我需要显示相同数据的第二个表.我似乎无法如此显示.我也尝试过修改代码.请帮忙.这是我所拥有的:
Here''s the code to display the data in the table 1. I need to display the second table of the same data. I cannot seem to display as such. I have tried to modify the code as well. Please help. Here''s what I have:
using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace UncFunc
{
class Report
{
public void Create(Quantity q)
{
Excel.Application objApp;
Excel._Workbook objBook;
Excel.Workbooks objBooks;
Excel.Sheets objSheets;
Excel._Worksheet objSheet;
Excel.Range range;
Excel.Range range1;
Dictionary<int, Quantity> rootQty = q.RootQuantities;
//object misValue = System.Reflection.Missing.Value;
try
{
// Instantiate Excel and start a new workbook.
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add(Type.Missing);
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
//Get the range where the starting cell has the address
//m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
range = objSheet.get_Range("A1", Type.Missing);
range = range.get_Resize(6,10);
//Create an array.
string[,] saRet = new string[rootQty.Count+2,10];
saRet[0, 4] = "Root Quantities";
saRet[1, 0] = "String";
saRet[1, 1] = "Symbol";
saRet[1, 2] = "Limit Description";
saRet[1, 3] = "Limit Type";
saRet[1, 4] = "Value";
saRet[1, 5] = "Limit";
saRet[1, 6] = "DF";
saRet[1, 7] = "Divisor";
saRet[1, 8] = "Std Unc";
saRet[1, 9] = "Unit";
//2nd table
range1 = objSheet.get_Range("A9", Type.Missing);
range1 = range1.get_Resize(6, 10);// 6,10
string[,] saRet1 = new string[rootQty.Count+2,10];
saRet[5, 0] = "New Table";
int[] aryKeys= new int[rootQty.Keys.Count];
Dictionary<int,Quantity>.KeyCollection oKeys = rootQty.Keys;
oKeys.CopyTo(aryKeys, 0);
//Fill the array.
for (long iRow = 0; iRow < rootQty.Count; iRow++)
{
//for (long iCol = 0; iCol < 4; iCol++)
//{
int iKey = aryKeys[iRow];
Quantity qty = rootQty[iKey];
String sSymbol = qty.Symbol;//column B
String sLimitDescription = qty.LimitDescription;// column C
//UncType = qty.LimitType.ToString();
String sValue = qty.Value.ToString();//column D
String sLimit = qty.Limit.ToString();//column E
String sDF = qty.df.ToString();//column F
String sDivisor = qty.Divisor.ToString();//column G
String sStdUnc = qty.StdUnc.ToString();//column H
String sUnit = qty.Unit;// column I
saRet[iRow+2, 1] = sSymbol;
saRet[iRow + 2, 2] = sLimitDescription;
//saRet[iRow + 2, 3] = qty.LimitType.ToString();
saRet[iRow + 2, 4] = sValue.ToString();
saRet[iRow + 2, 5] = sLimit.ToString();
saRet[iRow + 2, 6] = sDF.ToString();
saRet[iRow + 2, 7] = sDivisor.ToString();
saRet[iRow + 2, 8] = sStdUnc.ToString();
saRet[iRow + 2, 9] = sUnit;
//Previous statements
//saRet[iRow+2,0] = sSymbol;
//saRet[iRow+2,1] = sValue;
//saRet[iRow+2,2] = sStdUnc;
//saRet[iRow+2,3] = qty.Unit; //sUnit
//Put the row and column address in the cell.
//saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();
// }
}
//Set the range value to the array.
range.set_Value(Type.Missing, saRet);
//Replace the _ with space
string myValue = "X_2";
string newValue = myValue.Replace("_", "");
//Set Subscript
range = range.get_Resize(6, 9);
objSheet.get_Range("A1", Type.Missing);
//range = range.get_Resize(0, 2);
//range.get_Range(2, 0);
objSheet.get_Range("A1", "A6").EntireColumn.AutoFit();
objSheet.get_Range("B3", "B6").get_Characters(2, 2).Font.Subscript = true;//column and character chosen respectively
//2nd table
//objSheet.get_Range("A10", "A16").EntireColumn.AutoFit();
//objSheet.get_Range("B12", "B15").get_Characters(2, 2).Font.Subscript = true;//column and character chosen respectively
//objSheet.get_Range("B4","B6").Font.Subscript = true;
//objSheet.get_Range(1, 1).Font.Subscript = true;
//Set Bold(Added)
objSheet.get_Range("A1", "J1").Font.Bold = true;
objSheet.get_Range("A2", "J2").Font.Bold = true;
//range = range.get_Resize(1, 1);
//range.get_Characters(1,1).Font.Bold = true;
//range.Font.Bold = true;
//Return control of Excel to the user.
objApp.Visible = true;
objApp.UserControl = true;
// Displaying formula in spreadsheet
//objSheet.get_Range("A8", "A8").FormulaR1C1 = "y = x1 * x2 - x3 / x2 * Quantity.Log(x4)";
//border for the table
objSheet.get_Range("A1", "J6").BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium,
Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
//grouping of data
Excel.Range myRange = objSheet.Rows["3:6", Type.Missing] as Excel.Range;
//myRange.OutlineLevel = 1;
myRange.Group(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//Save
objBook.SaveAs("Report Uncertainty.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
Console.WriteLine(errorMessage, "Error");
}
}
推荐答案
这篇关于在一张纸中两次显示相同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!