使用visual studio 2010并拥有Excel报告生成器

时间:2015-01-31 03:19:49

标签: c# excel visual-studio-2010

我有很长的一系列数字(例如10000203005595000),当我将值传递给excel时,我有一个(例如10000E ^ + 10)值,C#中的代码应该操作格式单元格/数/分类/分数。要具有特定的价值。请帮助我,谢谢: - )

这是我的程序:`

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;

namespace BIS
{
    public partial class labDB : Form
    {
        System.Data.DataTable data;

        public labDB()
        {
            InitializeComponent();
        } 
        //this is my report generator using Excel
        private void genReport_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = "C:";
            saveFileDialog1.Title = "Save as Excel File";
            saveFileDialog1.FileName = "Laboratory Department Inventory Report";
            saveFileDialog1.Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xlsx|Excel Files(2010)|*.xlsx|Excel Files(2013)|*.xlsx";
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
                ExcelApp.Application.Workbooks.Add(Type.Missing);

                ExcelApp.Columns.ColumnWidth = 25;

                for (int i = 1; i < dataGridViewLAB.Columns.Count + 1; i++)
                {
                    ExcelApp.Cells[1, i] = dataGridViewLAB.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < dataGridViewLAB.Rows.Count; i++)
                {
                    for (int j = 0; j < dataGridViewLAB.Columns.Count; j++)
                    {
                        ExcelApp.Cells[i + 2, j + 1] = dataGridViewLAB.Rows[i].Cells[j].Value.ToString();
                    }
                }
                ExcelApp.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName.ToString());
                ExcelApp.ActiveWorkbook.Saved = true;
                ExcelApp.Quit();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

             using System;  
             using System.Collections.Generic;   
             using System.Data;  
             using System.Linq;  
             using System.Text;  
             using System.Threading.Tasks;  
             using System.Windows;  
             using System.Windows.Forms;  
             using Office = Microsoft.Office.Core;  
             using Excel = Microsoft.Office.Interop.Excel;

             namespace Excel   
             {   
                 public class ExcelUtlity   
                 {                      

    public bool WriteDataTableToExcel(System.Data.DataTable dataTable, string worksheetName, string saveAsLocation, string ReporType)        {
        Microsoft.Office.Interop.Excel.Application excel;
        Microsoft.Office.Interop.Excel.Workbook excelworkBook;
        Microsoft.Office.Interop.Excel.Worksheet excelSheet;
        Microsoft.Office.Interop.Excel.Range excelCellrange;
        try
        {
            // Start Excel and get Application object.
           excel = new Microsoft.Office.Interop.Excel.Application();
            // for making Excel visible
            excel.Visible = false;
            excel.DisplayAlerts = false;
            // Creation a new Workbook
            excelworkBook = excel.Workbooks.Add(Type.Missing);
            // Workk sheet             
            excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkBook.ActiveSheet;
            excelSheet.Name = worksheetName;
            excelSheet.Cells[1, 1] = ReporType;
            excelSheet.Cells[1, 2] = "Date : " + DateTime.Now.ToShortDateString();               
            // loop through each row and add values to our sheet
            int rowcount = 2;
            foreach (DataRow datarow in dataTable.Rows)
            {
                rowcount += 1;
                for (int i = 1; i <= dataTable.Columns.Count; i++)
                {
                    // on the first iteration we add the column headers
                    if (rowcount == 3)
                    {
                        excelSheet.Cells[2, i] = dataTable.Columns[i-1].ColumnName;
                        excelSheet.Cells.Font.Color = System.Drawing.Color.Black;
                    }
                    excelSheet.Cells[rowcount, i] = datarow[i-1].ToString();
                    //for alternate rows
                    if (rowcount > 3)
                    {
                        if (i == dataTable.Columns.Count)
                        {
                            if (rowcount % 2 == 0)
                            {
                                excelCellrange = excelSheet.Range[excelSheet.Cells[rowcount, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];
                                FormattingExcelCells(excelCellrange, "#CCCCFF", System.Drawing.Color.Black,false);
                            }
                        }
                    }
                }
            }
            // now we resize the columns
            excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];
            excelCellrange.EntireColumn.AutoFit();
            Microsoft.Office.Interop.Excel.Borders border = excelCellrange.Borders;
            border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
            border.Weight = 2d;
            excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[2, dataTable.Columns.Count]];
            FormattingExcelCells(excelCellrange, "#000099", System.Drawing.Color.White, true);
            //now save the workbook and exit Excel
            excelworkBook.SaveAs(saveAsLocation);;
            excelworkBook.Close();
            excel.Quit();
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
        finally
        {
            excelSheet = null;
            excelCellrange = null;
            excelworkBook = null;
        }
    }       
    /// FUNCTION FOR FORMATTING EXCEL CELLS

    public void FormattingExcelCells(Microsoft.Office.Interop.Excel.Range range, string HTMLcolorCode, System.Drawing.Color fontColor, bool IsFontbool)
    {
        range.Interior.Color = System.Drawing.ColorTranslator.FromHtml(HTMLcolorCode);
        range.Font.Color = System.Drawing.ColorTranslator.ToOle(fontColor);
        if (IsFontbool == true)
        {
            range.Font.Bold = IsFontbool;
        }
    }
}

}