• 大小: 0.07M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-04-28
  • 语言: C#
  • 标签: 文本  朗读  文字  

资源简介


资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace test
{
    class ExcelHelper
    {
        //将datagridview导出excel中
        public void dataToExcel(DataGridView dataGridView1)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = “Excel文件|*.xlsx|Excel(2003文件)|*.xls“;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string path = fileDialog.FileName;
                Excel.Application application = new Excel.Application();
                Excel.Workbooks workbooks = application.Workbooks;
                Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
                Excel.Worksheet worksheet = workbook.Worksheets[1];
                int colIndex = 0;
                //导出DataGridView中的标题
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (dataGridView1.Columns[i].Visible)//做同于不导出隐藏列
                    {
                        colIndex++;
                        worksheet.Cells[1 colIndex] = dataGridView1.Columns[i].HeaderText;
                    }
                }
                //导出DataGridView中的数据
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    colIndex = 0;
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        if (dataGridView1.Columns[j].Visible)
                        {
                            colIndex++;
                            worksheet.Cells[i + 2 colIndex] = “‘“ + dataGridView1.Rows[i].Cells[j].Value;
                        }
                    }
                }
                //保存文件
                workbook.SaveAs(fileDialog.FileName);
                application.Quit();
                MessageBox.Show(“导出成功“);
            }
        }
        //将excl导入datatable中
        public static DataTable inputExcel()
        {
            OpenFileDialog open = new OpenFileDialog();
            open.title = “打开Excel文件“;
            open.Filter = “Excel文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*“;
            string path = ““;
            open.RestoreDirectory = true;
            if (open.ShowDialog() == DialogResult.OK)
            {
                path = open.FileName;
            }
            try
            {

                //连接字符串
                string connstring = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=“ + path + “;Extended Properties=‘Excel 8.0;HDR=NO;IMEX=1‘;“; // Office 07及以上版本 不能出现多余的空格 而且分号注意
                //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=“ + path + “;Extended Properties=‘Excel 8.0;HDR=NO;IMEX=1‘;“; //Office 07以下版本 
                using (OleDbConnection conn 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      23552  2020-09-30 15:51  test\test\bin\Debug\test.exe

     文件      32256  2020-09-30 15:51  test\test\bin\Debug\test.pdb

     文件      11600  2020-09-30 15:51  test\test\bin\Debug\test.vshost.exe

     文件        490  2017-09-29 21:43  test\test\bin\Debug\test.vshost.exe.manifest

     文件       4132  2020-09-30 15:43  test\test\ExcelHelper.cs

     文件        690  2020-09-30 15:51  test\test\Form1.cs

     文件       6125  2020-09-30 15:51  test\test\Form1.Designer.cs

     文件      12935  2020-09-30 15:51  test\test\Form1.resx

     文件        508  2020-09-30 15:45  test\test\Form2.cs

     文件       2907  2020-09-30 15:45  test\test\Form2.Designer.cs

     文件       5817  2020-09-30 15:45  test\test\Form2.resx

     文件       5420  2020-09-30 15:39  test\test\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6455  2020-09-30 15:51  test\test\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        525  2020-09-30 15:51  test\test\obj\x86\Debug\test.csproj.FileListAbsolute.txt

     文件       1033  2020-09-30 15:51  test\test\obj\x86\Debug\test.csproj.GenerateResource.Cache

     文件      10611  2020-09-30 15:49  test\test\obj\x86\Debug\test.csprojResolveAssemblyReference.cache

     文件      23552  2020-09-30 15:51  test\test\obj\x86\Debug\test.exe

     文件       5151  2020-09-30 15:51  test\test\obj\x86\Debug\test.Form1.resources

     文件        180  2020-09-30 15:45  test\test\obj\x86\Debug\test.Form2.resources

     文件      32256  2020-09-30 15:51  test\test\obj\x86\Debug\test.pdb

     文件        180  2020-09-30 15:45  test\test\obj\x86\Debug\test.Properties.Resources.resources

     文件        485  2020-09-30 15:49  test\test\Program.cs

     文件       1340  2020-09-22 13:00  test\test\Properties\AssemblyInfo.cs

     文件       2860  2020-09-22 13:00  test\test\Properties\Resources.Designer.cs

     文件       5612  2020-09-22 13:00  test\test\Properties\Resources.resx

     文件       1091  2020-09-22 13:00  test\test\Properties\Settings.Designer.cs

     文件        249  2020-09-22 13:00  test\test\Properties\Settings.settings

     文件       4433  2020-09-30 15:43  test\test\test.csproj

     文件        854  2020-09-22 13:00  test\test.sln

    ..A..H.     22528  2020-09-30 15:52  test\test.suo

............此处省略12个文件信息

评论

共有 条评论