• Excel Programming (C# + VBA) Part III


     

    Step 6 Import the data in excel to database

    a.       Create excel app object and open the uploaded file

                Microsoft.Office.Interop.Excel.Application xlsApp = new ApplicationClass();

                Workbook wb 
    = xlsApp.Workbooks.Open(_filePath,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing, Type.Missing,Type.Missing,Type.Missing,Type.Missing);

                wb.Unprotect(TemplatePassword);

     

    b.      Save the upload file as a temporary file. Then close uploaded file and open the temporary file

               string tempFileName = _filePath.ToLower().Replace(".xls","_Temp.xls");      

                wb.Unprotect(TemplatePassword);

                wb.SaveCopyAs(tempFileName);

     

    c.       Verify the data in template again in web application

                ReadDataSource((Worksheet)wb.Worksheets[DataSourceSheet], out branchID, out planDate, out startDate, out endDate, out iChecked, out templatetype);

               
    if(!(iChecked == 1))

                
    {

                    returnVal 
    = "Please verify the data before upload to the server!";

                      
    throw new Exception(returnVal);

                }


                
    if(templatetype.ToUpper() != _templatetype.ToUpper())

                
    {

                      returnVal 
    = "The version is not corrected, please verify the document and uploaded again";

                      
    throw new Exception(returnVal);

                }


     ……

    private void ReadDataSource(Worksheet ws, out string branchID, out string planDate, out DateTime startDate,out DateTime endDate, out int iChecked, out string templatetype)

          
    {

                
    string check = ws.get_Range("A1", System.Type.Missing).Text.ToString();

                branchID 
    = ws.get_Range("A2", System.Type.Missing).Text.ToString();

                
    string sDate = ws.get_Range("A4", System.Type.Missing).Text.ToString();

                
    string eDate = ws.get_Range("A5", System.Type.Missing).Text.ToString();

                planDate 
    = ws.get_Range("A6", System.Type.Missing).Text.ToString();

                templatetype 
    = ws.get_Range("A7", System.Type.Missing).Text.ToString();

                startDate 
    = DateTime.Parse(sDate);

                endDate 
    = DateTime.Parse(eDate);

                
    try

                
    {

                      iChecked 
    = Convert.ToInt16(check);

                }


                
    catch

                
    {

                      iChecked 
    = 0;

                }


          }
        

                

    d.      Read the data in the worksheet

                string territoryList = ws.get_Range("B"+ i.ToString(), System.Type.Missing).Text.ToString();

                
    string territoryIDList = ws.get_Range("AB" + i.ToString(), System.Type.Missing).Text.ToString();

                
    string category = ws.get_Range("E" + i.ToString(), System.Type.Missing).Text.ToString();

                
    string categoryID = ws.get_Range("AE" + i.ToString(), System.Type.Missing).Text.ToString();

    e.      Change the database based on data in excel sheet

                SqlHelper.ExecuteNonQuery(connnectionString, CommandType.Text, "insert into … ");                 

    Summary: Objects used in this sample

    a.       Excel.Application

                                                                   i.      Application.Workbooks.Open

                                                                 ii.      Quit

    b.      Excel.Workbook

                                                                   i.      SaveCopyAs

                                                                 ii.      Unprotect

                                                                iii.      Worksheets

                                                               iv.      Protect

                                                                 v.      .Names.Add

                                                               vi.      Close

    c.       Excel.WorkSheet

                                                                   i.      Unprotect

                                                                 ii.      Protect

                                                                iii.      .Hyperlinks.Add

    d.      Range

                                                                   i.      Value2

                                                                 ii.      Text

    e.      Cell, Cells

  • 相关阅读:
    ListView Item 里多种点击事件的用法
    dialog 设置maxHeight 最大高度
    php的json_encode不兼容JSON_UNESCAPED_UNICODE的解决方案
    java面试一定会遇到的56个面试题
    使用LinearLayout实现ListView,解决ListView和ScrollView滚动冲突
    检测是否是小米手机
    Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
    clone分支,修改文件本地commit后, push回原分支失败,处理方法
    SQL语句往Oracle数据库中插入日期型数据(to_date的用法)
    C# TimeSpan 计算时间差(时间间隔)
  • 原文地址:https://www.cnblogs.com/lyrix/p/976015.html
Copyright © 2020-2023  润新知