十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
Private Sub RowAdd() '這里以一個為例,多個自己添加就可以了
10年積累的成都網站建設、網站設計經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先制作網站后付款的網站建設流程,更有紅橋免費網站建設讓你可以放心的選擇與我們合作。
If DataGridView1.RowCount 0 Then '行數(shù)是否大于1
For Each dr As DataGridViewRow In DataGridView1.Rows
Dim ds As Object() = New Object(dr.Cells.Count - 1) {} '根據(jù)單元格個數(shù)創(chuàng)建數(shù)據(jù)
For i = 0 To dr.Cells.Count - 1
ds(i) = dr.Cells(i).Value '給數(shù)組賦值
Next
DataGridView7.Rows.Add(ds) '獲得的值新增行
Next
End If
End Sub
這是c#中的“屬性”
假如某個類中有一個成員變量(字段),一般是不允許外部訪問的,為了安全性
如果要訪問它,必須通過“屬性”來訪問,例如:
private int Id; //這是一個成員變量,private表示是私有的,外部不可訪問
public int ID
{
get { return id; } //當外部訪問“屬性”ID時,返回id的值
set { id = value; } //當外部為“屬性”ID賦值時,將id賦值為value,value就是外部為“屬性”ID所賦的值
}
PS:你可以在set和get中寫一些隱藏的邏輯來控制這個訪問和賦值的過程,這對外部是不可見的
比如
set {
if(value==0)
id = 1;
else
id=value;
}
這樣當外部將ID賦值為0時,id里的值實際上是1 6
將解析出來的結果寫入到
Excel。
解析csv文件代碼:
public static List readCsvFile(String argPath) throws FileNotFoundException, IOException {
CsvFileUtil util = new CsvFileUtil();
File cvsFile = new File(argPath);
List list = new ArrayList();
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader(cvsFile);
bufferedReader = new BufferedReader(fileReader);
String regExp = util.getRegExp();
System.out.println(regExp);
String strLine = "";
String str = "";
while ((strLine = bufferedReader.readLine()) != null) {
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(strLine);
List listTemp = new ArrayList();
while(matcher.find()) {
str = matcher.group();
str = str.trim();
if (str.endsWith(",")){
str = str.substring(0, str.length()-1);
str = str.trim();
}
if (str.startsWith("\"") str.endsWith("\"")) {
str = str.substring(1, str.length()-1);
if (util.isExisted("\"\"", str)) {
str = str.replaceAll("\"\"", "\"");
}
}
if (!"".equals(str)) {
//test
System.out.print(str+" : ");
listTemp.add(str);
}
}
System.out.println();
list.add((String[]) listTemp.toArray(new String[listTemp.size()]));
}
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
throw e;
}
}
return list;
}
寫入Excel代碼:
public void exportExcel(String title, String[] headers,
CollectionT dataset, OutputStream out, String pattern) {
// 聲明一個工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一個表格
HSSFSheet sheet = workbook.createSheet(title);
// 設置表格默認列寬度為15個字節(jié)
sheet.setDefaultColumnWidth((short) 15);
// 生成一個樣式
HSSFCellStyle style = workbook.createCellStyle();
// 設置這些樣式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一個字體
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字體應用到當前的樣式
style.setFont(font);
// 生成并設置另一個樣式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一個字體
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字體應用到當前的樣式
style2.setFont(font2);
// 聲明一個畫圖的頂級管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定義注釋的大小和位置,詳見文檔
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
// 設置注釋內容
comment.setString(new HSSFRichTextString("可以在POI中添加注釋!"));
// 設置注釋作者,當鼠標移動到單元格上是可以在狀態(tài)欄中看到該內容.
comment.setAuthor("leno");
//產生表格標題行
HSSFRow row = sheet.createRow(0);
for (short i = 0; i headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//遍歷集合數(shù)據(jù),產生數(shù)據(jù)行
HSSFFont font3 = workbook.createFont();
font3.setColor(HSSFColor.BLUE.index);
IteratorT it = dataset.iterator();
int index = 0;
while (it.hasNext()) {
index++;
row = sheet.createRow(index);
T t = (T) it.next();
//利用反射,根據(jù)javabean屬性的先后順序,動態(tài)調用getXxx()方法得到屬性值
Field[] fields = t.getClass().getDeclaredFields();
for (short i = 0; i fields.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style2);
Field field = fields[i];
String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName,new Class[] {});
Object value = getMethod.invoke(t, new Object[] {});
//判斷值的類型后進行強制類型轉換
String textValue = null;
//其它數(shù)據(jù)類型都當作字符串簡單處理
textValue = value.toString();
System.out.println(getMethodName+" : "+textValue+" : "+getMethod+" : "+value);
//如果不是圖片數(shù)據(jù),就利用正則表達式判斷textValue是否全部由數(shù)字組成
if(textValue!=null){
Pattern p = Pattern.compile("^\\d+(\\.\\d+)?$");
Matcher matcher = p.matcher(textValue);
if(matcher.matches()){
//是數(shù)字當作double處理
cell.setCellValue(Double.parseDouble(textValue));
}
else{
HSSFRichTextString richString = new HSSFRichTextString(textValue);
richString.applyFont(font3);
cell.setCellValue(richString);
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally {
//清理資源
}
}
}
try {
workbook.write(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}