0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

cscの作法 その343

Last updated at Posted at 2023-06-05

概要

cscの作法、調べてみた。
練習問題、やってみた。

練習問題

closedxmlでsqliteを流し込んだ、xlsxを作れ。

写真

image.png

サンプルコード


using System;
using System.Text;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Transactions;
using System.Data.SQLite;
using ClosedXML.Excel;

namespace App
{
	class Program {
		static void Main(string[] args) {
			var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Book8.xlsx");
			var wb = new XLWorkbook();
			var ws = wb.Worksheets.Add("Sheet1");
			int i = 1;
			ws.Cell(i, 1).Value = "Id";
			ws.Cell(i, 2).Value = "Name";
			ws.Cell(i, 3).Value = "Number";
			ws.Cell(i, 4).Value = "Price";
			SQLiteConnectionStringBuilder ConnectionStr = new SQLiteConnectionStringBuilder();
			ConnectionStr.DataSource = "db0.sqlite";
			using (SQLiteConnection Connection = new SQLiteConnection(ConnectionStr.ToString()))
			{
				Connection.Open();
				using(SQLiteCommand Command = new SQLiteCommand(Connection))
				{
					Command.CommandText = "SELECT * FROM fruit ORDER BY id ASC";
					using (SQLiteDataReader Reader = Command.ExecuteReader())
					{
						while (Reader.Read())
						{
							i++;
							string Id = Reader.GetInt32(0).ToString();
							string Name = Reader.GetString(1);
							string Number = Reader.GetInt32(2).ToString();
							string Price = Reader.GetInt32(3).ToString();
							ws.Cell(i, 1).Value = Id;
							ws.Cell(i, 2).Value = Name;
							ws.Cell(i, 3).Value = Number;
							ws.Cell(i, 4).Value = Price;
						}
					}
				}
			}
			wb.SaveAs(filePath);
			Console.WriteLine("ok");
		}
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?