请高人看下这段代码 c# 在创建excel sheet时为什么会失败?

时间:2022-09-14 19:16:49

bool exist = System.IO.File.Exists(@"C:\ConfigRecord.xls");

if(exist)
{
    System.IO.File.Delete(@"C:\ConfigRecord.xls");
}

object missing = System.Reflection.Missing.Value; 

Excel.ApplicationClass myExcel = new Excel.ApplicationClass();   
  
Excel._Workbook xBk ;                //工作薄  
   
xBk = myExcel.Workbooks.Add(true); 


xBk.SaveAs(@"C:\ConfigRecord.xls",missing,missing, 
missing,missing,missing,Excel.XlSaveAsAccessMode.xlShared, 
missing,missing,missing,missing,missing); 

    
myExcel.Quit();


OleDbConnection excelConn = new OleDbConnection();
excelConn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\ConfigRecord.xls; Extended Properties=Excel 8.0;"; 

excelConn.Open(); 

OleDbCommand oleExcelCommand=new OleDbCommand (); 
oleExcelCommand.Connection =excelConn; 



string basicInfotableName;
basicInfotableName="deviceInfo_BaseInfo";
sql="drop table " + basicInfotableName + ";";
oleExcelCommand.CommandText=sql;
try
{
  oleExcelCommand.ExecuteNonQuery();
}
catch(System.Data.OleDb.OleDbException ex)
{

}

sql="create table "+basicInfotableName+" (DeviceType char, WholeDeviceCode char primary key";
sql=sql+");";
oleExcelCommand.CommandText=sql;

try
{
  oleExcelCommand.ExecuteNonQuery();
}
catch(System.Data.OleDb.OleDbException ex)
{
MessageBox.Show(this,"创建数据库失败","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}

总是提示“创建数据库失败”
说未找到表deviceInfo_BaseInfo

11 个解决方案

#1


单步调一下.把相应的sql语句考到SQL2000里面试试,可以成功执行吗?

#2


摘录:《程序员秘书》--源代码--目录及文件--读取Excel文件--生成一个Exel文件
你直接用就行了
立即成为编程经验丰富的程序员不是梦,详见:http://www.psec.net.cn

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
        SaveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
        if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string[,] myData = {
            {"车牌号","类 型","品 牌","型 号","颜 色","附加费证号","车架号"},
            {"浙KA3676","危险品","货车","铁风SZG9220YY","白","1110708900","022836"},
            {"浙KA4109","危险品","货车","解放CA4110P1K2","白","223132","010898"},
            {"浙KA0001A","危险品","货车","南明LSY9190WS","白","1110205458","0474636"},
            {"浙KA0493","上普货","货车","解放LSY9190WS","白","1110255971","0094327"},
            {"浙KA1045","普货","货车","解放LSY9171WCD","蓝","1110391226","0516003"},
            {"浙KA1313","普货","货车","解放9190WCD","蓝","1110315027","0538701"},
            {"浙KA1322","普货","货车","解放LSY9190WS","蓝","24323332","0538716"},
            {"浙KA1575","普货","货车","解放LSY9181WCD","蓝","1110314149","0113018"},
            {"浙KA1925","普货","货车","解放LSY9220WCD","蓝","1110390626","00268729"},
            {"浙KA2258","普货","货车","解放LSY9220WSP","蓝","111048152","00320"}};

            Excel.Application m_Excel = new Excel.Application();//创建一个Excel对象(同时启动EXCEL.EXE进程)
            m_Excel.SheetsInNewWorkbook = 2;//工作表为两个
            Excel._Workbook m_Book = (Excel._Workbook)(m_Excel.Workbooks.Add(Missing.Value));//添加新工作簿
            Excel._Worksheet m_Sheet = (Excel._Worksheet)(m_Book.Worksheets.get_Item(1));//读取工作表1
            m_Sheet.Name = "车牌表";//给工作表1命名
            m_Sheet.Cells[1, 4] = "普通报表";//第一行为报表名称
            for (int i = 0; i < 11; i++)//逐行写入数据
            {
                for (int j = 0; j < 7; j++)
                {
                    m_Sheet.Cells[2 + i, 1 + j] = "'" + myData[i, j];//以单引号开头,表示该单元格为纯文本
                }
            }
            m_Book.SaveAs(SaveFileDialog1.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            m_Book.Close(false, Missing.Value, Missing.Value);
            m_Excel.Quit();
            m_Book = null;
            m_Sheet = null;
            m_Excel = null;
            MessageBox.Show(this, "Excel文件已经生成。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    catch (Exception Mye)
    {
        MessageBox.Show(this, Mye.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    GC.Collect();//强制垃圾回收,否则EXCEL.EXE进程不能及时退出
}

#3


你看看你之前执行的删除操作是否成功了..

#4


删除操作也失败了 为什么呢?

#5


一步一步跟踪调试,F11,看看是那句话出的问题?

#6


删除操作都失败了 应该和excel没有关系 确认一下你的文件位置

#7


Mark

#8


去掉你的try和catch,看一看系统提示的错误信息

#9


自己跟踪调试一下吧..应该和excel没有什么关系..

#10


1.看看你的Excel文件是否生成
2.看看你的Excel文件格式是否是纯Excel格式文件
导入Excel可以查看
http://www.wsoft.net/Index/Catalog53/225.aspx

#11


如果文件生成了,你直接用企业管理器导入Excel文件,看能否成功!不成功说明格式有问题。

#1


单步调一下.把相应的sql语句考到SQL2000里面试试,可以成功执行吗?

#2


摘录:《程序员秘书》--源代码--目录及文件--读取Excel文件--生成一个Exel文件
你直接用就行了
立即成为编程经验丰富的程序员不是梦,详见:http://www.psec.net.cn

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
        SaveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
        if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string[,] myData = {
            {"车牌号","类 型","品 牌","型 号","颜 色","附加费证号","车架号"},
            {"浙KA3676","危险品","货车","铁风SZG9220YY","白","1110708900","022836"},
            {"浙KA4109","危险品","货车","解放CA4110P1K2","白","223132","010898"},
            {"浙KA0001A","危险品","货车","南明LSY9190WS","白","1110205458","0474636"},
            {"浙KA0493","上普货","货车","解放LSY9190WS","白","1110255971","0094327"},
            {"浙KA1045","普货","货车","解放LSY9171WCD","蓝","1110391226","0516003"},
            {"浙KA1313","普货","货车","解放9190WCD","蓝","1110315027","0538701"},
            {"浙KA1322","普货","货车","解放LSY9190WS","蓝","24323332","0538716"},
            {"浙KA1575","普货","货车","解放LSY9181WCD","蓝","1110314149","0113018"},
            {"浙KA1925","普货","货车","解放LSY9220WCD","蓝","1110390626","00268729"},
            {"浙KA2258","普货","货车","解放LSY9220WSP","蓝","111048152","00320"}};

            Excel.Application m_Excel = new Excel.Application();//创建一个Excel对象(同时启动EXCEL.EXE进程)
            m_Excel.SheetsInNewWorkbook = 2;//工作表为两个
            Excel._Workbook m_Book = (Excel._Workbook)(m_Excel.Workbooks.Add(Missing.Value));//添加新工作簿
            Excel._Worksheet m_Sheet = (Excel._Worksheet)(m_Book.Worksheets.get_Item(1));//读取工作表1
            m_Sheet.Name = "车牌表";//给工作表1命名
            m_Sheet.Cells[1, 4] = "普通报表";//第一行为报表名称
            for (int i = 0; i < 11; i++)//逐行写入数据
            {
                for (int j = 0; j < 7; j++)
                {
                    m_Sheet.Cells[2 + i, 1 + j] = "'" + myData[i, j];//以单引号开头,表示该单元格为纯文本
                }
            }
            m_Book.SaveAs(SaveFileDialog1.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            m_Book.Close(false, Missing.Value, Missing.Value);
            m_Excel.Quit();
            m_Book = null;
            m_Sheet = null;
            m_Excel = null;
            MessageBox.Show(this, "Excel文件已经生成。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    catch (Exception Mye)
    {
        MessageBox.Show(this, Mye.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    GC.Collect();//强制垃圾回收,否则EXCEL.EXE进程不能及时退出
}

#3


你看看你之前执行的删除操作是否成功了..

#4


删除操作也失败了 为什么呢?

#5


一步一步跟踪调试,F11,看看是那句话出的问题?

#6


删除操作都失败了 应该和excel没有关系 确认一下你的文件位置

#7


Mark

#8


去掉你的try和catch,看一看系统提示的错误信息

#9


自己跟踪调试一下吧..应该和excel没有什么关系..

#10


1.看看你的Excel文件是否生成
2.看看你的Excel文件格式是否是纯Excel格式文件
导入Excel可以查看
http://www.wsoft.net/Index/Catalog53/225.aspx

#11


如果文件生成了,你直接用企业管理器导入Excel文件,看能否成功!不成功说明格式有问题。