DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Java UDFs and Stored Procedures for Data Engineers: A Hands-On Guide
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • Practical Generators in Go 1.23 for Database Pagination

Trending

  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • A Developer's Guide to Mastering Agentic AI: From Theory to Practice
  • Grafana Loki Fundamentals and Architecture
  1. DZone
  2. Data Engineering
  3. Data
  4. How to Copy Data from a Range of Cells In Java Apps

How to Copy Data from a Range of Cells In Java Apps

Trying to figure out how to copy data from a range of cells in Java apps? Learn how in this quick snippet.

By 
David Zondray user avatar
David Zondray
·
Aug. 26, 15 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
12.7K Views

Join the DZone community and get the full member experience.

Join For Free

This technical tip show how developers can Copy Range Data with Style in an Excel Workbook inside Java Applications. Copy Range Data Only explained how to copy the data from a range of cells to another range. Aspose.Cells can also copy a range complete with formatting. This article explains how to achieve this task. Aspose.Cells provides a range of classes and methods for working with ranges, for example createRange(), StyleFlag, applyStyle() etc. This example explains how to:

  • Creates a workbook.
  • Fills a number of cells in the first worksheet with data.
  • Creates a range.
  • Creates a style object with specified formatting attributes.
  • Applies the style to the data range.
  • Creates a second range of cells.
  • Copies data with the formatting from the first range to the second range.

Copy a Range of Data with Style  in a Workbook

[Java]

String filePath = "F:/Downloads/source.xlsx";

//Load your source workbook
Workbook workbook = new Workbook(filePath);

//0-byte array
byte[] workbookData = new byte[0];

//Text save options. You can use any type of separator
TxtSaveOptions opts = new TxtSaveOptions();
opts.setSeparator('\t');

//Copy each worksheet data in text format inside workbook data array
for (int idx = 0; idx < workbook.getWorksheets().getCount(); idx++)
{
    //Save the active worksheet into text format
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    workbook.getWorksheets().setActiveSheetIndex(idx);
    workbook.save(bout, opts);

    //Save the worksheet data into sheet data array
    byte[] sheetData = bout.toByteArray();

    //Combine this worksheet data into workbook data array
    byte[] combinedArray = new byte[workbookData.length + sheetData.length];
    System.arraycopy(workbookData, 0, combinedArray, 0, workbookData.length);
    System.arraycopy(sheetData, 0, combinedArray, workbookData.length, sheetData.length);

    workbookData = combinedArray;
}

//Save entire workbook data into file
FileOutputStream fout = new FileOutputStream(filePath + ".out.txt");
fout.write(workbookData);
fout.close();
Data (computing) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java UDFs and Stored Procedures for Data Engineers: A Hands-On Guide
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • Practical Generators in Go 1.23 for Database Pagination

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: