Click here to Skip to main content
15,890,557 members
Home / Discussions / C#
   

C#

 
GeneralRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Glen Childs30-Sep-13 6:05
Glen Childs30-Sep-13 6:05 
GeneralRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Richard MacCutchan30-Sep-13 6:15
mveRichard MacCutchan30-Sep-13 6:15 
GeneralRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Glen Childs30-Sep-13 6:19
Glen Childs30-Sep-13 6:19 
SuggestionRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Richard MacCutchan30-Sep-13 6:46
mveRichard MacCutchan30-Sep-13 6:46 
GeneralRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Jason Gleim30-Sep-13 7:52
professionalJason Gleim30-Sep-13 7:52 
GeneralRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Richard Deeming30-Sep-13 8:19
mveRichard Deeming30-Sep-13 8:19 
GeneralRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Glen Childs30-Sep-13 22:43
Glen Childs30-Sep-13 22:43 
AnswerRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Richard Deeming1-Oct-13 1:33
mveRichard Deeming1-Oct-13 1:33 
Start by reading the sheet:
C#
static DataTable LoadExcelSheet(string fileName, int worksheetNumber, bool headers)
{
    if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName");
    if (!File.Exists(fileName)) throw new FileNotFoundException(null, fileName);

    var cnnStr = string.Format(
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR={1};IMEX=1\"", 
        fileName, 
        headers ? "Yes" : "No");
    
    using (var cnn = new OleDbConnection(cnnStr))
    {
        cnn.Open();

        var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        
        // NB: The schema table often contains two rows per sheet;
        // you need to ignore any which end with "$":
        var sheetRow = schemaTable.AsEnumerable()
            .Where(row => !((string)row["TABLE_NAME"]).EndsWith("$"))
            .Skip(worksheetNumber).FirstOrDefault();
        
        if (sheetRow == null) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet");

        string sql = string.Format("SELECT * FROM [{0}$]", sheetRow["TABLE_NAME"]);
        var da = new OleDbDataAdapter(sql, cnn);

        var dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
}


That will give you a DataTable containing the data from the Excel sheet. You'll then need to use a SqlConnection and a SqlDataAdapter to update your SQL database.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: I am getting an error when trying to save an XLS to .DBF file. Pin
Glen Childs1-Oct-13 3:10
Glen Childs1-Oct-13 3:10 
QuestionSQL XML TYPE Pin
Member 1030679730-Sep-13 4:44
Member 1030679730-Sep-13 4:44 
AnswerRe: SQL XML TYPE Pin
Eddy Vluggen30-Sep-13 7:29
professionalEddy Vluggen30-Sep-13 7:29 
GeneralRe: SQL XML TYPE Pin
Member 103067971-Oct-13 4:44
Member 103067971-Oct-13 4:44 
QuestionSetting a default and maximum number in a textbox. New to C#. Please help Pin
Mandyy1429-Sep-13 19:58
Mandyy1429-Sep-13 19:58 
SuggestionRe: Setting a default and maximum number in a textbox. New to C#. Please help Pin
Richard MacCutchan29-Sep-13 21:15
mveRichard MacCutchan29-Sep-13 21:15 
GeneralRe: Setting a default and maximum number in a textbox. New to C#. Please help Pin
PIEBALDconsult30-Sep-13 5:09
mvePIEBALDconsult30-Sep-13 5:09 
GeneralRe: Setting a default and maximum number in a textbox. New to C#. Please help Pin
Amol_B30-Sep-13 23:14
professionalAmol_B30-Sep-13 23:14 
QuestionImplement Undo/Redo - Copy/Paste on DesignSurface Pin
olograph29-Sep-13 10:18
olograph29-Sep-13 10:18 
AnswerRe: Implement Undo/Redo - Copy/Paste on DesignSurface Pin
Brisingr Aerowing29-Sep-13 10:45
professionalBrisingr Aerowing29-Sep-13 10:45 
QuestionBeginning C# journey - knowledge and tips... Pin
Bankaida29-Sep-13 9:44
Bankaida29-Sep-13 9:44 
AnswerRe: Beginning C# journey - knowledge and tips... Pin
David C# Hobbyist.29-Sep-13 11:44
professionalDavid C# Hobbyist.29-Sep-13 11:44 
GeneralRe: Beginning C# journey - knowledge and tips... Pin
Gergo Bogdan30-Sep-13 2:13
Gergo Bogdan30-Sep-13 2:13 
GeneralRe: Beginning C# journey - knowledge and tips... Pin
David C# Hobbyist.30-Sep-13 2:44
professionalDavid C# Hobbyist.30-Sep-13 2:44 
AnswerRe: Beginning C# journey - knowledge and tips... Pin
Richard MacCutchan29-Sep-13 20:56
mveRichard MacCutchan29-Sep-13 20:56 
AnswerRe: Beginning C# journey - knowledge and tips... Pin
Glen Childs30-Sep-13 0:35
Glen Childs30-Sep-13 0:35 
QuestionSSRS Reporting USING Oracle Database Pin
JYOTISANKAR MOHAPATRA29-Sep-13 8:38
JYOTISANKAR MOHAPATRA29-Sep-13 8:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.