我使用以下方法在表格数据旁边创建和定位Excel图表。下面的代码工作正常,但图表位置(高度)在图表数量增加时会产生问题。
绘制图表有细胞值的解决方案吗?或者我可以使用的其他东西来保存表数据旁边的图表。
我想逐页打印以下方法的结果,因此要正确放置。
下面是我的代码:
Excel.Application xla = new Excel.Application();
int height=75;
string uppercell = "B12";
int cell = 12;
if (CheckBox1.Checked == true)
{
string table_name, chart_name;
table_name = "Ration Table";
chart_name = "Ratio";
string st = "";
con.Open();
trying(ws, st, height, uppercell, cell, table_name, chart_name);
con.Close();
cell = cell + 33;
uppercell = "B" + cell;
height = 496 + 75;//height + 300;
}
public void trying(Excel.Worksheet ws,string st,double height,string uppercell,int cell,string table_name,string chart_name)
{
MySqlDataAdapter da = new MySqlDataAdapter(st, con);
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
//int cell = System.Int32.Parse(uppercell.Substring(1));
//********************** Now create the chart. *****************************
Excel.ChartObjects chartObjs = (Excel.ChartObjects)ws.ChartObjects(Type.Missing);
//Excel.ChartObject chartObj = chartObjs.Add(left, top, width, height);
Excel.ChartObject chartObj = chartObjs.Add(260, height, 350, 210);
Excel.Chart xlChart = chartObj.Chart;
//ws.Shapes.Item("xlChart").Top = (float)(double)ws.get_Range("E6").Top;//this.Controls.AddChart(this.Range["D2", "H12"]"chart1");
int nRows = dtMainSQLData.Rows.Count;
int nColumns = dtMainSQLData.Columns.Count;
string upperLeftCell = uppercell;// "B10";
int endRowNumber = System.Int32.Parse(upperLeftCell.Substring(1)) + nRows - 1;
char endColumnLetter = System.Convert.ToChar(Convert.ToInt32(upperLeftCell[0]) + nColumns - 1);
string upperRightCell = System.String.Format("{0}{1}", endColumnLetter, System.Int32.Parse(upperLeftCell.Substring(1)));
string lowerRightCell = System.String.Format("{0}{1}", endColumnLetter, endRowNumber);
Excel.Range rg = ws.get_Range(upperLeftCell, lowerRightCell);
for (int i = cell - 1; i < dtMainSQLData.Rows.Count + cell; i++)
{
for (int j = 2; j < dtMainSQLData.Columns.Count + 2; j++)
{
if (i == cell - 1)
{
xla.Cells[i, j] = dcCollection[j - 2].ToString();
}
else
{
xla.Cells[i, j] = dtMainSQLData.Rows[i - cell][j - 2].ToString();
}
}
}
//ws.Columns.AutoFit();
//for (int i = 1; i <= dtMainSQLData.Rows.Count; i++)
//{
// rg[1, i] = dtMainSQLData.Rows[i - 1][0].ToString(); //For Adding Header Text
// rg[2, i] = int.Parse(dtMainSQLData.Rows[i - 1][1].ToString()); //For Adding Datarow Value
//}
int lcellh = cell - 10;
int rcellh = cell - 10;
string lcellh1 = "B" + lcellh;
string rcellh1 = "L" + rcellh;
//-----------------------for company name and address-------------------------//
Excel.Range chartRange3;
ws.get_Range(lcellh1, rcellh1).Merge(false);//----------------------------------upperleft_cell, lowerright_cell
chartRange3 = ws.get_Range(lcellh1, rcellh1);
chartRange3.FormulaR1C1 = "BLUE BIRD FOODS (I) PVT.LTD.";//----------------------------------company name
chartRange3.HorizontalAlignment = 3;
chartRange3.VerticalAlignment = 3;
chartRange3.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
chartRange3.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.PaleVioletRed);
chartRange3.Font.Size = 20;
Excel.Range formatRange3;
formatRange3 = ws.get_Range(lcellh1, rcellh1);//----------------------------------hrow_border1, hrow_border2
formatRange3.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
//------for making header font bold
int hcell = cell - 1;
string hleftrange = "B" + hcell;
string hrightrange = "D" + hcell;
Excel.Range formatRange1;
formatRange1 = ws.get_Range(hleftrange);//----------------------------------hrow_bold
formatRange1.EntireRow.Font.Bold = true;
formatRange1.HorizontalAlignment = 3;
formatRange1.VerticalAlignment = 3;
//ws.Cells[1, 3] = "Bold";
//-------for giving broder to header
Excel.Range formatRange2;
formatRange2 = ws.get_Range(hleftrange, hrightrange);//----------------------------------hrow_border1, hrow_border2
formatRange2.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
//-------for giving broder to table data
Excel.Range formatRange;
formatRange = ws.get_Range(upperLeftCell, lowerRightCell);//----------------------------------upperLeftCell, lowerRightCell
formatRange.BorderAround(Excel.XlLineStyle.xlContinuous,
Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic,
Excel.XlColorIndex.xlColorIndexAutomatic);
formatRange.HorizontalAlignment = 3;
formatRange.VerticalAlignment = 3;
//--------for giving name to the table
int lcell = cell - 3;
int rcell = cell - 2;
string tleftrange = "B" + lcell;
string trightrange = "D" + rcell;
Excel.Range chartRange1;
ws.get_Range(tleftrange, trightrange).Merge(false);//----------------------------------upperleft_cell, lowerright_cell
formatRange2 = ws.get_Range(tleftrange, trightrange);
formatRange2.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
chartRange1 = ws.get_Range(tleftrange, trightrange);
chartRange1.FormulaR1C1 = table_name;//"Ratio table";//----------------------------------table_name
chartRange1.HorizontalAlignment = 3;
chartRange1.VerticalAlignment = 3;
chartRange1.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
chartRange1.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.PaleVioletRed);
chartRange1.Font.Size = 20;
//--------drawing chart with range
Excel.Range chartRange = ws.get_Range(upperLeftCell, lowerRightCell);
xlChart.SetSourceData(chartRange, System.Reflection.Missing.Value);
xlChart.ChartType = Excel.XlChartType.xl3DPie;
// *******************Customize axes: ***********************
Excel.Axis xAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlCategory,
Excel.XlAxisGroup.xlPrimary);
//xAxis.HasTitle = true;
// xAxis.AxisTitle.Text = "X Axis";
Excel.Axis yAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlSeriesAxis,
Excel.XlAxisGroup.xlPrimary);
//yAxis.HasTitle = true;
//yAxis.AxisTitle.Text = "Y Axis";
Excel.Axis zAxis = (Excel.Axis)xlChart.Axes(Excel.XlAxisType.xlValue,
Excel.XlAxisGroup.xlPrimary);
//zAxis.HasTitle = true;
//zAxis.AxisTitle.Text = "Z Axis";
// *********************Add title: *******************************
xlChart.HasTitle = true;
xlChart.ChartTitle.Text = chart_name;// "Ratio";
// *****************Set legend:***************************
xlChart.HasLegend = true;
}