Importing data into к from excel

###Chapter 3 Importing Excel Data ###List the Sheets of an Excel File # Load the readxl package library(readxl) # Print out the names of both spreadsheets excel_sheets(«urbanpop.xlsx«) ###Import an Excel Sheet # The readxl package is already loaded # Read the sheets, one by one pop_1 <- read_excel(«urbanpop.xlsx«, sheet = 1) pop_2 <- read_excel(«urbanpop.xlsx«, sheet = 2) pop_3 <- read_excel(«urbanpop.xlsx«, sheet = 3) # Put pop_1, pop_2 and pop_3 in a list: pop_list pop_list <- list(pop_1, pop_2, pop_3) # Display the structure of pop_list str(pop_list) ###Reading a Workbook # The readxl package is already loaded # Read all Excel sheets with lapply(): pop_list pop_list <- lapply(excel_sheets(«urbanpop.xlsx«), read_excel, path = «urbanpop.xlsx«) # Display the structure of pop_list str(pop_list) ###The col_names Argument # The readxl package is already loaded # Import the the first Excel sheet of urbanpop_nonames.xlsx (R gives names): pop_a pop_a <- read_excel(«urbanpop_nonames.xlsx«, col_names = FALSE) # Import the the first Excel sheet of urbanpop_nonames.xlsx (specify col_names): pop_b cols <- c(«country«, paste0(«year_«, 1960:1966)) pop_b <- read_excel(«urbanpop_nonames.xlsx«, col_names = cols) # Print the summary of pop_a summary(pop_a) # Print the summary of pop_b summary(pop_b) ###The skip Argument # The readxl package is already loaded # Import the second sheet of urbanpop.xlsx, skipping the first 21 rows: urbanpop_sel urbanpop_sel <- read_excel(«urbanpop.xlsx«, sheet = 2, col_names = FALSE, skip = 21) # Print out the first observation from urbanpop_sel urbanpop_sel[1,] ###Import a Local File # Load the gdata package library(gdata) # Import the second sheet of urbanpop.xls: urban_pop urban_pop <- read.xls(«urbanpop.xls«, sheet = «1967-1974«) # Print the first 11 observations using head() head(urban_pop, n = 11) ###read.xls() Wraps Around read.table() # The gdata package is alreaded loaded # Column names for urban_pop columns <- c(«country«, paste0(«year_«, 1967:1974)) # Finish the read.xls call urban_pop <- read.xls(«urbanpop.xls«, sheet = 2, skip = 50, header = FALSE, stringsAsFactors = FALSE, col.names = columns) # Print first 10 observation of urban_pop head(urban_pop, n = 10) ###Work that Excel Data! # Import all sheets from urbanpop.xls path <- «urbanpop.xls« urban_sheet1 <- read.xls(path, sheet = 1, stringsAsFactors = FALSE) urban_sheet2 <- read.xls(path, sheet = 2, stringsAsFactors = FALSE) urban_sheet3 <- read.xls(path, sheet = 3, stringsAsFactors = FALSE) # Extend the cbind() call to include urban_sheet3: urban_all urban <- cbind(urban_sheet1, urban_sheet2[1], urban_sheet3[1]) # Remove all rows with NAs from urban: urban_clean urban_clean <- na.omit(urban) # Print out a summary of urban_clean summary(urban_clean)

Importing Data from Excel

Excel is a spreadsheet application, which is widely used by many institutions to store data. This tutorial will give a brief of reading, writing and manipulating the data in Excel files using R. We will learn about various R packages and extensions to read and import Excel files. At the end of this section, we have written about some common problems encountered while loading Excel files and spreadsheet data.

Before we import the data from Excel spreadsheet into R, there are some standard practices to tone your data, to avoid any unnecessary error.

  • The first column of the spreadsheet is used to identify the sample dataset, therefore it should be a unique key id. Similarly the first row is reserved for header, describing the scheme of the data.
  • Concatenating words in the cells should be done using ‘.’. For example, ‘Sample.data’.
  • The names and header of the data scheme should usually avoid symbols.
  • All missing data points in the Excel spreadsheet should be indicated with ‘NA’.

Before you import the Excel data in R, you would need to set the console in R to working directory.

>getwd()//To get the working directory at the moment
>setwd(“”)

 Before we look into the packages available to extract data from Excel spreadsheet, we will show you simple R commands that can do the job. Utlis package is one of the core packages which contains bunch of basic utility functions and the following commands are part of this package.

Access Solved Big Data and Data Science Projects

  • read.table()
dataset <-read.table(“”,
                     header =TRUE)

The first argument of read.table() function is the name of the text file within the double quotes and if the data file has a header for data schema in the top row, the second argument will be true. This function will work for files, which are saved in .txt format.

Learn Data Science by working on interesting Data Science Projects 

Reading data from an excel file is incredibly easy and it can be done using several packages. You can export the Excel file to a Comma delimited file and import it using the method shown in the tutorial Importing Data from Flat Files in R. Another method to Import Data in R from Excel is using xlsx package, which I used to access Excel files. The first row should contain variable names.

//read in the excel sheet from workbook sample_excel.xls
//variable name in the first row
library(xlsx)
sampledata<- read.xlsx(“sample_excel.xls”,
                       sheetName=”sample_sheet1”)

It is necessary that while using read.xlsx function, we mention the sheet index or the sheet name. If the required dataset is bigger, then read.xlsx2() function is used.

Sample.data <- read.xlsx2(“sample_excel.xls”,
                          sheetName=”sample_sheeet1”,
                          startRow = 100,
                          colIndex = 100)

Real-time Solved Machine Learning

Additionally in the function above, user can mention the end row or the data import can be limited to certain row and column index. xlsx package does a lot more than importing data from Excel files, it can also manipulate the data and write data frames into the spreadsheets. The data frames can be written to Excel workbook using the function write.xlsx().

>write.xlsx(Sample.data,
            “Sample_Sheet.xls”,
            sheetName=”sample_sheet1”)

Apart from the xlsx package, we have gdata package, which has functions that can read from data in the Excel format. gdata provides a cross platform solution for importing data from Excel files into R. The read.xls function in particular can read data from an Excel spreadsheet and gives data frame as output. Take for example a sample Excel spreadsheet, named ‘Sample_Sheet.xls’ and to use this method, you would require Perl runtime in your system.

>library(gdata)//Load gdata package
>sample_data = read.xls(“Sample_Sheet.xls”)//Read data from the sheet

This function converts the Sample_Sheet.xls file into a temporary .csv or .tab limited file using Perl. While executing read.xls function, R will search for a path to the excel file and looks out for Perl on its way. If it doesn’t find perl.exe, then R will return an error. To avoid this error, another argument for the function can be given to search for the Perl executable file.

>sample.data <- read.xlsx(“Sample_Sheet.xlsx,
                          sheetIndex = 1,
                          perl = “C:/Perl/bin/perl.exe””)

gdata has several other functions to convert the Excel file into various other formats. Such as:

  • xls2sep()
  • xls2csv()
  • xls2tab()
  • xls2tsv()

The input arguments for these functions are same as that for read.xlsx() function.

Another package that can do the job of importing data from Excel is the XLConnect package using the loadWorkbook function. This function can be used to read the entire workbook, followed by readWorksheet function to load the worksheets into R. Java is required to be pre-installed for this package to work. This package also provides function to create Excel workbooks, and export data to them.

>library(XLConnect)
>Sample_Workbook = loadWorkbook(“Sample_Sheet.xls”)
>Sample_Data = readWorksheet (Sample_Workbook,
                              sheet=”Sheet1”)

Other arguments can also be added after the Index argument such as startCol or StartRow or endCol or endRow to indicate and limit the cells that are required to be imported from the Excel workbook. Another argument ‘region’ can also be used in this function to highlight the range of starting and ending rows and columns.

Machine Learning Projects 

  • Preleminary tasks
  • Copying data from Excel and import into R
    • On Windows system
    • On Mac OSX system
  • Importing Excel files into R using readxl package
    • Installing and loading readxl package
    • Using readxl package
  • Importing Excel files using xlsx package
    • Installing and loading xlsx package
    • Using xlsx package
    • Read more
  • Summary
  • Related articles
  • Infos

Previously, we described the essentials of R programming and some best practices for preparing your data. We also provided quick start guides for reading and writing txt and csv files using R base functions as well as using a most modern R package named readr, which is faster (X10) than R base functions.

In this article, you’ll learn how to read data from Excel xls or xlsx file formats into R. This can be done either by:

  • copying data from Excel
  • using readxl package
  • or using xlsx package

Reading Data From Excel Files (xls|xlsx) into R

Preleminary tasks

  1. Launch RStudio as described here: Running RStudio and setting up your working directory

  2. Prepare your data as described here: Best practices for preparing your data

Copying data from Excel and import into R

On Windows system

  1. Open the Excel file containing your data: select and copy the data (ctrl + c)

  2. Type the R code below to import the copied data from the clipboard into R and store the data in a data frame (my_data):

my_data <- read.table(file = "clipboard", 
                      sep = "t", header=TRUE)

On Mac OSX system

  1. Select and copy the data (Cmd + c)

  2. Use the function pipe(pbpaste) to import the data you’ve copied (with Cmd + c):

my_data <- read.table(pipe("pbpaste"), sep="t", header = TRUE)

Importing Excel files into R using readxl package

The readxl package, developed by Hadley Wickham, can be used to easily import Excel files (xls|xlsx) into R without any external dependencies.

Installing and loading readxl package

  • Install
install.packages("readxl")
  • Load
library("readxl")

Using readxl package

The readxl package comes with the function read_excel() to read xls and xlsx files

  1. Read both xls and xlsx files
# Loading
library("readxl")
# xls files
my_data <- read_excel("my_file.xls")
# xlsx files
my_data <- read_excel("my_file.xlsx")

The above R code, assumes that the file “my_file.xls” and “my_file.xlsx” is in your current working directory. To know your current working directory, type the function getwd() in R console.

  • It’s also possible to choose a file interactively using the function file.choose(), which I recommend if you’re a beginner in R programming:
my_data <- read_excel(file.choose())

If you use the R code above in RStudio, you will be asked to choose a file.

  1. Specify sheet with a number or name
# Specify sheet by its name
my_data <- read_excel("my_file.xlsx", sheet = "data")
  
# Specify sheet by its index
my_data <- read_excel("my_file.xlsx", sheet = 2)
  1. Case of missing values: NA (not available). If NAs are represented by something (example: “—”) other than blank cells, set the na argument:
my_data <- read_excel("my_file.xlsx", na = "---")

Importing Excel files using xlsx package

The xlsx package, a java-based solution, is one of the powerful R packages to read, write and format Excel files.

Installing and loading xlsx package

  • Install
install.packages("xlsx")
  • Load
library("xlsx")

Using xlsx package

There are two main functions in xlsx package for reading both xls and xlsx Excel files: read.xlsx() and read.xlsx2() [faster on big files compared to read.xlsx function].

The simplified formats are:

read.xlsx(file, sheetIndex, header=TRUE)
read.xlsx2(file, sheetIndex, header=TRUE)

  • file: file path
  • sheetIndex: the index of the sheet to be read
  • header: a logical value. If TRUE, the first row is used as column names.

Example of usage:

library("xlsx")
my_data <- read.xlsx(file.choose(), 1)  # read first sheet

Summary

  • Read Excel files using readxl package: read_excel(file.choose(), sheet = 1)

  • Read Excel files using xlsx package: read.xlsx(file.choose(), sheetIndex = 1)

Related articles

  • Previous chapters
    • R programming basics
    • Best practices in preparing data files for importing into R
    • Reading data from txt|csv files: R base functions
    • Fast Reading of Data From txt|csv Files into R: readr package
  • Next chapters
    • Exporting data from R

Infos

This analysis has been performed using R (ver. 3.2.3).

Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below… Thank you and please don’t forget to share and comment below!!

Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l’envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d’amour avec les like ci-dessous … Merci et n’oubliez pas, s’il vous plaît, de partager et de commenter ci-dessous!

Import from an Excel Table

Your browser does not support video. Install Microsoft Silverlight, Adobe Flash Player, or Internet Explorer 9.

When you load the sales data into Power Query and apply some transformations, Power Query creates a new worksheet, but the original sales data worksheet stays the same. This makes it easy to experiment with the data without changing the data source.

  1. Notice that there are product categories and sales data worksheets. Open the Sales Data worksheet.

  2. Position the cursor on the Excel table, Select Data > Get & Transform Data > From Table/Range.

    Excel opens the Power Query Editor with your data displayed in a preview pane.

  3. To display all query tables in the workbook from the Queries pane, select the arrow to the left of the preview pane.

    The Product Sales table is listed last, then the World Cup Results table, and then the tables used earlier in the tutorial workbook. As you add new tables, they are automatically listed here.

    1. The Query Settings pane shows the transformation steps you have taken. You can preview, change, and delete any steps. Power Query has added a few steps for you. The following table summarizes important features:

      Feature

      Description

      Source

      Defines the source for your data.

      Changed Type

      Power Query interprets the data types. You can adjust these later.

      Home Tab

      Use to preview your data and make transformations.

      Close & Load

      When finished, select to return the data to the worksheet.

      Transform Tab

      Provides advanced transformation options.

      Add Column

      Calculates data from existing columns, such as the day of the week from a date or custom calculations.

      View Tab

      Provides additional options, such as opening the Advanced Query Editor.

  4. To return the transformed data to Excel, select Home > Close & Load.

Note   Whether or not you transformed the data, a new worksheet is created. It’s a good idea to rename the new worksheet to clearly distinguish it from the original worksheet. Additional transformations are always added to the new worksheet.

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

Can any one explain how to import a Microsoft Excel file in to a MySQL database?

For example, my Excel table looks like this:

Country   |   Amount   |   Qty
----------------------------------
America   |   93       |   0.60

Greece    |   9377     |   0.80

Australia |   9375     |   0.80

jarrodwhitley's user avatar

asked Aug 21, 2009 at 5:07

Fero's user avatar

11

There’s a simple online tool that can do this called sqlizer.io.

Screenshot from sqlizer.com

You upload an XLSX file to it, enter a sheet name and cell range, and it will generate a CREATE TABLE statement and a bunch of INSERT statements to import all your data into a MySQL database.

(Disclaimer: I help run SQLizer)

answered Sep 22, 2014 at 16:40

d4nt's user avatar

d4ntd4nt

15.3k9 gold badges40 silver badges51 bronze badges

9

Below is another method to import spreadsheet data into a MySQL database that doesn’t rely on any extra software. Let’s assume you want to import your Excel table into the sales table of a MySQL database named mydatabase.

  1. Select the relevant cells:

    enter image description here

  2. Paste into Mr. Data Converter and select the output as MySQL:

    enter image description here

  3. Change the table name and column definitions to fit your requirements in the generated output:

CREATE TABLE sales (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  Country VARCHAR(255),
  Amount INT,
  Qty FLOAT
);
INSERT INTO sales
  (Country,Amount,Qty)
VALUES
  ('America',93,0.60),
  ('Greece',9377,0.80),
  ('Australia',9375,0.80);
  1. If you’re using MySQL Workbench or already logged into mysql from the command line, then you can execute the generated SQL statements from step 3 directly. Otherwise, paste the code into a text file (e.g., import.sql) and execute this command from a Unix shell:

    mysql mydatabase < import.sql

    Other ways to import from a SQL file can be found in this Stack Overflow answer.

Community's user avatar

answered Mar 24, 2017 at 4:16

thdoan's user avatar

thdoanthdoan

18.3k1 gold badge58 silver badges51 bronze badges

2

  1. Export it into some text format. The easiest will probably be a tab-delimited version, but CSV can work as well.

  2. Use the load data capability. See http://dev.mysql.com/doc/refman/5.1/en/load-data.html

  3. Look half way down the page, as it will gives a good example for tab separated data:

    FIELDS TERMINATED BY ‘t’ ENCLOSED BY » ESCAPED BY »

  4. Check your data. Sometimes quoting or escaping has problems, and you need to adjust your source, import command— or it may just be easier to post-process via SQL.

ashatte's user avatar

ashatte

5,4128 gold badges38 silver badges50 bronze badges

answered Aug 21, 2009 at 5:13

ndp's user avatar

ndpndp

21.4k5 gold badges35 silver badges52 bronze badges

6

There are actually several ways to import an excel file in to a MySQL database with varying degrees of complexity and success.

  1. Excel2MySQL. Hands down, the easiest and fastest way to import Excel data into MySQL. It supports all verions of Excel and doesn’t require Office install.

    screenshot of Excel2MySQL

  2. LOAD DATA INFILE: This popular option is perhaps the most technical and requires some understanding of MySQL command execution. You must manually create your table before loading and use appropriately sized VARCHAR field types. Therefore, your field data types are not optimized. LOAD DATA INFILE has trouble importing large files that exceed ‘max_allowed_packet’ size. Special attention is required to avoid problems importing special characters and foreign unicode characters. Here is a recent example I used to import a csv file named test.csv.

    enter image description here

  3. phpMyAdmin: Select your database first, then select the Import tab. phpMyAdmin will automatically create your table and size your VARCHAR fields, but it won’t optimize the field types. phpMyAdmin has trouble importing large files that exceed ‘max_allowed_packet’ size.

    enter image description here

  4. MySQL for Excel: This is a free Excel Add-in from Oracle. This option is a bit tedious because it uses a wizard and the import is slow and buggy with large files, but this may be a good option for small files with VARCHAR data. Fields are not optimized.

    enter image description here

answered Aug 30, 2014 at 1:50

panofish's user avatar

panofishpanofish

7,51013 gold badges54 silver badges96 bronze badges

5

Not sure if you have all this setup, but for me I am using PHP and MYSQL. So I use a PHP class PHPExcel. This takes a file in nearly any format, xls, xlsx, cvs,… and then lets you read and / or insert.

So what I wind up doing is loading the excel in to a phpexcel object and then loop through all the rows. Based on what I want, I write a simple SQL insert command to insert the data in the excel file into my table.

On the front end it is a little work, but its just a matter of tweaking some of the existing code examples. But when you have it dialed in making changes to the import is simple and fast.

answered Oct 6, 2014 at 18:26

user1441213's user avatar

1

the best and easiest way is to use «MySQL for Excel» app that is a free app from oracle. this app added a plugin to excel to export and import data to mysql. you can download that from here

answered May 26, 2015 at 7:03

Sadeq Shajary's user avatar

When using text files to import data, I had problems with quotes and how Excel was formatting numbers. For example, my Excel configuration used the comma as decimal separator instead of the dot.

Now I use Microsoft Access 2010 to open my MySql table as linked table. There I can simply copy and paste cells from Excel to Access.

To do this, first install the MySql ODBC driver and create an ODBC connection.
Then in access, in the «External Data» tab, open «ODBC Database» dialog and link to any table using the ODBC connection.

Using MySql Workbench, you can also copy and paste your Excel data into the result grid of MySql Workbench. I gave detailed instructions in this answer.

answered Nov 12, 2014 at 10:39

Christophe Weis's user avatar

Christophe WeisChristophe Weis

2,4984 gold badges28 silver badges32 bronze badges

Fastest and simpliest way is to save XLS as ODS (open document spreasheet) and import it from PhpMyAdmin
enter image description here

answered Nov 15, 2020 at 5:41

Daniel D's user avatar

Daniel DDaniel D

791 silver badge4 bronze badges

For a step by step example for importing Excel 2007 into MySQL with correct encoding (UTF-8) search for this comment:

«Posted by Mike Laird on October 13 2010 12:50am»

in the next URL:

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

answered Aug 12, 2014 at 19:52

Raúl Moreno's user avatar

Raúl MorenoRaúl Moreno

3133 silver badges14 bronze badges

You could use DocChow, a very intuitive GIU for importing Excel into MySQL, and it’s free on most common platforms (including Linux).

More especially if you are concerned about date, datetime datatypes, DocChow easily handles datatypes. If you are working with multiple Excel spreadsheets that you want to import into one MySQL table DocChow does the dirty work.

answered Dec 15, 2014 at 21:19

Seanj1000's user avatar

Step 1 Create Your CSV file

Step 2 log in to your mysql server

     mysql -uroot -pyourpassword 

Step 3
load your csv file

     load data local infile '//home/my-sys/my-excel.csv' into table my_tables fields terminated by ',' enclosed by '"' (Country, Amount,Qty);

answered Oct 5, 2018 at 12:07

Syed Shibli's user avatar

Syed ShibliSyed Shibli

9821 gold badge12 silver badges15 bronze badges

0

Another useful tool, and as a MySQL front-end replacement, is Toad for MySQL. Sadly, no longer supported by Quest, but a brilliant IDE for MySQL, with IMPORT and EXPORT wizards, catering for most file types.

answered Jul 26, 2019 at 1:17

Fandango68's user avatar

Fandango68Fandango68

4,2893 gold badges38 silver badges69 bronze badges

If you are using Toad for MySQL steps to import a file is as follows:

  1. create a table in MySQL with the same columns that of the file to be imported.
  2. now the table is created, goto > Tools > Import > Import Wizard
  3. now in the import wizard dialogue box, click Next.
  4. click Add File, browse and select the file to be imported.
  5. choose the correct dilimination.(«,» seperated for .csv file)
  6. click Next, check if the mapping is done properly.
  7. click Next, select the «A single existing table» radio button also select the table that to be mapped from the dropdown menu of Tables.
  8. Click next and finish the process.

answered Dec 24, 2020 at 12:15

tushar_lokare's user avatar

tushar_lokaretushar_lokare

4611 gold badge8 silver badges22 bronze badges

If you don’t like plugins, VBA and external tools, I have an excel file that using formulas only allows you to create INSERT/UPDATES. You only have to put the data on the cells:

enter image description here

As an extra, there’s another tab in the file to CREATE TABLES:

enter image description here

The file can be found on the following link:

EXCEL FILE

answered Apr 19, 2021 at 3:58

Kaiser's user avatar

KaiserKaiser

1,9271 gold badge19 silver badges27 bronze badges

I’ve had good results with the Tools / Import CSV File feature in HeidiSQL, with CSV files directly exported from Excel 2019 with «Save As…»

It uses LOAD DATA INFILE internally but with a GUI interface and also analyzes the CSV file before passing it to LOAD DATA INFILE so it can, for example, create the table using the first row as column names and guessing the column data type (<New table> option as shown in the picture)

enter image description here

answered Apr 11, 2022 at 7:46

golimar's user avatar

golimargolimar

2,3711 gold badge22 silver badges33 bronze badges

Like this post? Please share to your friends:
  • Importing csv files to excel
  • Importing csv and excel
  • Importing contacts from excel
  • Importantly is not a word
  • Important word meaning of english