当前位置:网站首页>【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
【C#笔记】winform中保存DataGridView中的数据为Excel和CSV
2022-07-02 06:26:00 【寂云萧】
保存为Excel
使用system.io保存,需要导入的库:using System.IO;
//使用system.io保存
//grid为整个DataGridView对象,而filePath为要保存的路径(包括后缀名),下同
public void saveExcelIO(DataGridView grid, string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
StringBuilder sb = new StringBuilder();
//写入标题
for(int k = 0; k < grid.Columns.Count; k++)
{
if (grid.Columns[k].Visible)
{
//\t相当于是在键盘上打一个tab键
sb.Append(grid.Columns[k].HeaderText.ToString().Trim() + "\t");
}
}
//换行
sb.Append(Environment.NewLine);
//开始写入每行的数值
for (int i = 0; i < grid.Rows.Count - 1; i++)
{
System.Windows.Forms.Application.DoEvents();
for(int j = 0; j < grid.Columns.Count; j++)
{
if (grid.Columns[j].Visible)
{
//单元格有一定字节的数量限制,可能会超出,超出会出现两个单元格内容一样的情况
sb.Append(grid.Rows[i].Cells[j].Value.ToString().Trim() + "\t");
}
}
sb.Append(Environment.NewLine);//换行
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}
保存为CSV
//保存csv
public void saveCSV(DataGridView grid,string filePath)
{
Thread.Sleep(1000);
StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("gb2312"));
string strLine = "";
//表头
for (int i = 0; i < grid.ColumnCount; i++)
{
if (i > 0)
strLine += ",";
strLine += grid.Columns[i].HeaderText;
}
strLine.Remove(strLine.Length - 1);
sw.WriteLine(strLine);
strLine = "";
for(int j = 0; j < grid.Rows.Count; j++)
{
strLine = "";
int col = grid.Columns.Count;
for(int k = 0; k < col; k++)
{
if (k > 0 && k < col)
strLine += ",";
if (grid.Rows[j].Cells[k].Value == null)
strLine += "";
else
{
string cell = grid.Rows[j].Cells[k].Value.ToString().Trim();
//防止里面有特殊符号
cell = cell.Replace("\"", "\"\"");
cell = "\"" + cell + "\"";
strLine += cell;
}
}
sw.WriteLine(strLine);
}
sw.Close();
}
边栏推荐
- 【Ranking】Pre-trained Language Model based Ranking in Baidu Search
- MoCO ——Momentum Contrast for Unsupervised Visual Representation Learning
- PointNet理解(PointNet实现第4步)
- 【AutoAugment】《AutoAugment:Learning Augmentation Policies from Data》
- 【Batch】learning notes
- [CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video
- Handwritten call, apply, bind
- [CVPR‘22 Oral2] TAN: Temporal Alignment Networks for Long-term Video
- 【Mixed Pooling】《Mixed Pooling for Convolutional Neural Networks》
- 【BiSeNet】《BiSeNet:Bilateral Segmentation Network for Real-time Semantic Segmentation》
猜你喜欢
![[model distillation] tinybert: distilling Bert for natural language understanding](/img/c1/e1c1a3cf039c4df1b59ef4b4afbcb2.png)
[model distillation] tinybert: distilling Bert for natural language understanding

点云数据理解(PointNet实现第3步)

【Programming】

A slide with two tables will help you quickly understand the target detection
![[tricks] whiteningbert: an easy unsupervised sentence embedding approach](/img/8e/3460fed55f2a21f8178e7b6bf77d56.png)
[tricks] whiteningbert: an easy unsupervised sentence embedding approach

The difference and understanding between generative model and discriminant model

【Sparse-to-Dense】《Sparse-to-Dense:Depth Prediction from Sparse Depth Samples and a Single Image》

【Cutout】《Improved Regularization of Convolutional Neural Networks with Cutout》

What if a new window always pops up when opening a folder on a laptop

Tencent machine test questions
随机推荐
【DIoU】《Distance-IoU Loss:Faster and Better Learning for Bounding Box Regression》
Using compose to realize visible scrollbar
Implement interface Iterable & lt; T&gt;
程序的执行
【TCDCN】《Facial landmark detection by deep multi-task learning》
【MnasNet】《MnasNet:Platform-Aware Neural Architecture Search for Mobile》
[paper introduction] r-drop: regulated dropout for neural networks
【双目视觉】双目立体匹配
Two dimensional array de duplication in PHP
TimeCLR: A self-supervised contrastive learning framework for univariate time series representation
Jordan decomposition example of matrix
生成模型与判别模型的区别与理解
[multimodal] clip model
基于onnxruntime的YOLOv5单张图片检测实现
图片数据爬取工具Image-Downloader的安装和使用
win10+vs2017+denseflow编译
【MagNet】《Progressive Semantic Segmentation》
Implementation of yolov5 single image detection based on pytorch
[Sparse to Dense] Sparse to Dense: Depth Prediction from Sparse Depth samples and a Single Image
PPT的技巧