C# chart控件导出为PNG图片并导出Excel到指定路径

public void ExportExcel(DataTable dt,String filePath)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                MessageBox.Show("无数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DataTable exportDT = dt.Copy();
                                               
                if (!filePath.EndsWith("."))
                {
                    filePath = filePath + ".";
                }
                filePath = filePath + "xlsx";
                string result = Util.Pub.ExportExcel(exportDT, filePath, true);
                //if (result == "")
                //{
                //    MessageBox.Show("导出成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
                //else
                //{
                //    MessageBox.Show("导出失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //}                
            }
        }

        public void fun()
        {
            DataTable dtFile001 = BLL.UtilClass.GetFilePath();
            if (dtFile001.Rows.Count > 0)
            {
                string fullFileName = "";

                fullFileName = dtFile001.Rows[0]["BCLJ"].ToString().Trim() + "\\" + comboBox1.Text.Trim().TrimEnd('c', 's', 'v');
                if (!fullFileName.EndsWith("."))
                {
                    fullFileName = fullFileName + ".";
                }
                fullFileName = fullFileName + "png";

                ExportExcel(dt, dtFile001.Rows[0]["BCLJ"].ToString().Trim() + "\\" + comboBox1.Text.Trim().TrimEnd('c', 's', 'v'));

                Bitmap bit = new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
                Graphics g = Graphics.FromImage(bit);
                g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
                g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
                //g.CopyFromScreen(panel1.PointToScreen(Point.Empty), Point.Empty, panel1.Size);//只保存某个控件(这里是panel游戏区)

                try
                {
                    bit.Save(fullFileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
                }
                catch(Exception ex)
                {
                    MessageBox.Show("未设置保存为文件路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            timer1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            fun();
        }


public static string ExportExcel(DataTable dt, string filename, bool ShowHead)
        {
            try
            {
                Workbook workbook = new Workbook(); //工作簿
                Worksheet sheet = workbook.Worksheets[0]; //工作表
                Cells cells = sheet.Cells;//单元格
                if (ShowHead)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        cells[0, i].PutValue(dt.Columns[i].ColumnName);
                    }
                }

                int N = ShowHead ? 1 : 0; ;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        cells[i + N, j].PutValue(dt.Rows[i][j].ToString());
                    }
                }
                workbook.Save(filename);
                
                return "";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

导出Excel的dll:

Aspose.Cells.dll

你可能感兴趣的:(C#)